Skip to:
Content
Pages
Categories
Search
Top
Bottom

If Profile Field is Not Empty, Set Profile Field Data?


  • ekko848
    Participant

    @ekko848

    I am trying to create a function in bp-custom.php where if a user inputs data into a profile field, another field is automatically set to “Pending” (from empty).

    I know I have to use xprofile_get_field_data to determine the field I wish to determine is empty or not empty.
    Then use an if statement along with xprofile_set_field_data to input the word or option to insert if this field is not empty (!empty).

    function profile_stream ( $user_id ) {
        $streamid = xprofile_get_field_data( 10, $user_id );
        if (!empty($streamid)) {
            xprofile_set_field_data( 11, $user_id, "Pending");
        }
    }

    MySQL table numbers for wp_bp_xprofile_fields are ’10’ and ’11’ as reflected in the code.

    Any tips on how to get this working?

Viewing 5 replies - 1 through 5 (of 5 total)

  • danbp
    Moderator

    @danbp

    Hi,

    no idea on how to conditionnaly combine 2 xprofile field.

    Here how you can show a msg if a field was not filed, even if it is not exactly what you asked for.

    if ( bp_is_active( 'xprofile' ) || bp_is_profile_component() || bp_is_user()  )
     	
    	if ( !$data = bp_get_profile_field_data( 'field=Service' ) ) : // use field name
    		echo '<br>Your profile is not complete. Please fill in field: Service<br>'; 				
    	endif;

    ekko848
    Participant

    @ekko848

    Thanks for the reply @danbp.

    Actually, I did some more testing.. and it appears my code is working correctly.

    I just forgot the

    add_action( 'xprofile_updated_profile', 'profile_stream');

    beneath it.

    Now if <field 10> has input, then <field 11> is automatically set to Pending.

    My issue now is that I cannot change it to anything other than pending! Cannot change it to “approved” or “rejected” because the code is making it always set to “Pending” whenever the profile is updated and that field is not empty..


    ekko848
    Participant

    @ekko848

    Here is my updated code

    function profile_stream ( $user_id ) {
        $streamid = xprofile_get_field_data( 10, $user_id );
        if (!empty($streamid)) {
            xprofile_set_field_data( 11, $user_id, "Pending");
        } if (empty($streamid)) {
            xprofile_set_field_data( 11, $user_id, "");
        }
    }
    
    add_action( 'xprofile_updated_profile', 'profile_stream');

    If field 10 is not empty, add the words “Pending” to field 11
    If field 10 is empty, make field 11 blank.

    Works perfectly.

    Anyone have any idea on how to restrict this function to normal users though? So it does not apply to an admin/moderator who can change “Pending” to whatever they want


    ekko848
    Participant

    @ekko848

    This seems to have worked

    function profile_stream ( $user_id ) {
        $streamid = xprofile_get_field_data( 10, $user_id );
        if (! bp_current_user_can( 'bp_moderate' ) && !empty($streamid)) {
            xprofile_set_field_data( 11, $user_id, "Pending");
        } if (! bp_current_user_can( 'bp_moderate' ) && empty($streamid)) {
            xprofile_set_field_data( 11, $user_id, "");
        }
    }
    
    add_action( 'xprofile_updated_profile', 'profile_stream');

    Considering I am relatively new to PHP and buddypress.. I am surprised I am able to get this stuff to work on my own lol.


    shanebp
    Moderator

    @shanebp

    just fyi: you can replace this:
    ! bp_current_user_can( 'bp_moderate' )

    with this:
    ! is_super_admin()

    is_super_admin()

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar