Skip to:
Content
Pages
Categories
Search
Top
Bottom

Remove add_friend button

  • Avatar of mika89
    mika89
    Participant

    @mika89

    Hi everyone,

    I’m trying to remove add_friend button on members loop only for memebrs who have a certain meta data that we’ll call ‘inactive’.

    Here’s my code:

    function hideAddFriend(){
    	$caps = get_user_meta(bp_get_member_user_id(), 'wp_capabilities', true);
    	$roles = array_keys((array)$caps);
    	// Hide friendship buttons
    	if(in_array("inactive", $roles)){
    		return ;
    	}	
    }
    add_filter( 'bp_get_add_friend_button', 'hideAddFriend');

    The thing is that function make disapear all the add_friend button of all members on the page and not only the ones whose the role is “inactive”.

    I m just starting to get how filters work, but I m quiet sure I m close to the solution. I just can’t understand how to specify which button will disapear…

    Thanks in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • Avatar of shanebpdev
    shanebp
    Participant

    @shanebp

    Filters have to return something.
    You’re only returning if a condition is met.
    So no buttons will appear.

    And you aren’t passing the $arg, so you can’t return it.

    Try:

    function hideAddFriend($button){
    	$caps = get_user_meta(bp_get_member_user_id(), 'wp_capabilities', true);
    	$roles = array_keys((array)$caps);
    	// Hide friendship buttons
    	if(in_array("inactive", $roles)){
    		return '';
    	}
            return $button;	
    }
    add_filter( 'bp_get_add_friend_button', 'hideAddFriend');

    also – are you sure that your get_user_meta approach to retrieving ‘inactive’ is working?

    Avatar of mika89
    mika89
    Participant

    @mika89

    Your code is working like a charm thank you :)
    But you must be right about the get_user_meta cause your code also remove the button from members who are not supposed to be “inactive”. How can I retrieve the role of a member ? Like if he’s an administrator, i want to get the string “administrator”.

    Thanks !

    Avatar of shanebpdev
    shanebp
    Participant

    @shanebp

    
    function hideAddFriend($button){
    	$role = mika_user_has_role( bp_get_member_user_id() );
    
    	if( $role )
    	     return '';
    
    	return $button;	
    }
    add_filter( 'bp_get_add_friend_button', 'hideAddFriend');
    
    function mika_user_has_role( $id, $role = 'inactive' ){
    	$roles = mika_get_user_roles( $id ); 
    	if ( $roles && in_array ( $role, $roles ) ) 
    		return true;
    	else 
    		return false;
    }
    
    function mika_get_user_roles( $id )  {
    	$user = new WP_User( $id ); 
    
    	if ( $user ) {
    		$roles = $user->roles; 
    		return $roles;
    	}  
    	else 
    		return false;
    }
    
    Avatar of mika89
    mika89
    Participant

    @mika89

    Thank u very much ! it’s exactly what I needed :)

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

You must be logged in to reply to this topic.