Conditionally hide profile field code

BP Profile Search Conditionally hide profile field code

  • Author
    Post
  • #12008 Reply
    Michael
    Guest

    Hi Andrea,

    Could you possibly assist me with code?
    I am currently using the code below to conditionally hide a specific profile field (=188) from displaying. All’s good, does what I want.

    
    function my_function( $retval ) {
    
        if ( $data = bp_get_profile_field_data( 'field=188' ) ) {
            if (//my conditional code here//) {
            $retval['exclude_fields'] = '188';
            }
        }
        return $retval;
    }
    add_filter( 'bp_after_has_profile_parse_args', 'my_function' );
    

    However, I also output that same field (=188) on the Members page using the BP Search shortcode:

    [bps_directory show=’field_2, field_189, field_127, field_188′]

    I would like to similarly conditionally remove that field=188 from the search results, using my same conditional code.

    Can you please assist?

    Thanks,

    Michael

    #12010 Reply
    andrea
    Plugin Author

    Hi Michael,

    You can add this code to your bp-custom.php file:

    add_filter ('bps_details', 'change_details');
    function change_details ($details)
    {
    	if (//my conditional code here//)
    		unset ($details['field_188']);
    	return $details;
    }
    #12013 Reply
    Michael
    Guest

    Thanks Andrea!
    But I need to apologize, I think I didn’t include enough information in my original request for help, sorry!

    Full info:

    – (//my conditional code//) is as follows:

    
    if ( bp_registration_get_moderation_status( bp_displayed_user_id() ) ) {
    

    This checks the moderation status (true/false) of the owner of the profile page being displayed. If true then field=188 is not shown.

    The moderation check function is part of BP Registration Options plugin.

    But I think this only works on the profile page? I want to check moderation status of users on the Members page and if moderation=true then not output field=188, else output it.

    Furthermore, I also have an active code snippet (kindly supplied by you!) which truncates field=188 down to 500 characters when it is output on the Members page. As follows:

    
    add_action ('bps_field_before_details', 'truncate_text');
    function truncate_text ($f)
    {
    if ($f->code == 'field_188' && strlen ($f->d_value) > 500)
    {
    $f->d_value = substr ($f->d_value, 0, 500). '<strong> ... [continues on&nbspProfile]</strong>';
    }
    }
    

    I hope this is enough information. Any assistance would – as always – be greatly appreciated.

    Michael.

    #12016 Reply
    andrea
    Plugin Author

    Hi Michael,

    In your conditional code:

    if ( bp_registration_get_moderation_status( bp_displayed_user_id() ) ) {
    

    bp_displayed_user_id() must be replaced by the user id currently displayed in the directory. You can try this:

    add_filter ('bps_details', 'change_details');
    function change_details ($details)
    {
    	global $members_template;
    
    	if (bp_registration_get_moderation_status( $members_template->member->ID ))
    		unset ($details['field_188']);
    	return $details;
    }
    #12017 Reply
    Michael
    Guest

    Wonderful, it worked perfectly. Thanks very much!

    #12018 Reply
    andrea
    Plugin Author

    Great, you’re welcome!

Reply to: Conditionally hide profile field code
My Information