BuddyDev

Search

[Resolved] Profile tabs depending to user role or member type

  • Participant
    Level: Enlightened
    Posts: 38
    Beuza on #11757

    Hi,

    I would like to hide a profile menu tab depending to user role or member type.
    I have 3 member types (Player, Agent, Scout) and i would like to display “Groups” only on Agent profile. I would like Player and Scout can see Groups menu when they look an Agent profile.
    Could someone help me please, i am working on it since 2 months without solution.

    Thanks,

    Beuza

  • Keymaster
    (BuddyDev Team)
    Posts: 21463
    Brajesh Singh on #11773

    Hi Beuza,
    Welcome to BuddyDev.

    I am sorry to hear that you were unable to find a solution. I will help as much as possible.

    First, I assume you already have the member types registered. Still, here is the code that I am using for registering member type for this example.

    
    
    /**
     * Register member types.
     */
    function buddydev_register_member_types() {
    	bp_register_member_type( 'player', array(
    		'labels'        => array(
    			'name'          => 'Players',
    			'singular_name' => 'Player',
    		),
    		'has_directory' => true,
    	) );
    
    	bp_register_member_type( 'agent', array(
    		'labels'        => array(
    			'name'          => 'Agents',
    			'singular_name' => 'Agent',
    		),
    		'has_directory' => true,
    	) );
    
    	bp_register_member_type( 'scout', array(
    		'labels'        => array(
    			'name'          => 'Scouts',
    			'singular_name' => 'Scout',
    		),
    		'has_directory' => true,
    	) );
    }
    add_action( 'bp_register_member_types', 'buddydev_register_member_types' );
    
    

    That registers member types. The important things to note is “agent”, “player” and “scout” is the actual name of member type(unique identifier) in this case. rest of the code will use them.

    Step 1:- Remove the groups form all user’s profile who don’t have the member type as ‘agent’

    
    
    /**
     * Remove groups from user profile based on member type.
     */
    function buddydev_remove_group_based_on_member_types() {
    	if ( ! bp_is_user() ) {
    		return;
    	}
    
    	$user_id = bp_displayed_user_id();
    
    	if ( ! bp_has_member_type( $user_id, 'agent' ) ) {
    		bp_core_remove_nav_item( 'groups' );
    	}
    
    }
    
    add_action( 'bp_setup_nav', 'buddydev_remove_group_based_on_member_types', 1001 );
    
    

    And it will only show the “Groups” nav on the user who have agent member type.

    We still have an issue. The adminbar for all users show “groups” and leads to page not found. let us fix that.

    Step 2:-

    
    
    /**
     * Remove groups from adminbar based on member type.
     *
     * @param array $nav nav items.
     *
     * @return array
     */
    function buddydev_filter_show_hide_groups_in_adminbar( $nav ) {
    	if ( ! is_user_logged_in() ) {
    		return $nav;
    	}
    
    	if ( ! bp_has_member_type( bp_loggedin_user_id(), 'agent' ) ) {
    		$nav = array();
    	}
    
    	return $nav;
    }
    
    add_filter( 'bp_groups_admin_nav', 'buddydev_filter_show_hide_groups_in_adminbar' );
    
    

    That’s all.

    Please put the code in your bp-custom.php and It will work.

    Hope you have a great time working with BuddyPress 🙂

    Regards
    Brajesh

    • This reply was modified 4 years, 8 months ago by Brajesh Singh. Reason: updating code
  • Participant
    Level: Enlightened
    Posts: 38
    Beuza on #11801

    Hi Brajesh,

    Thank you so much for your help. Unfortunatly for me, it doesn’t work. “Groups” nav continu to appear in all type of member profile. Do you want some access to see more?
    I pasted it in my bp-custom.php but nothing new happened:

    /**
    * Remove groups from user profile based on member type.
    */
    function buddydev_remove_group_based_on_member_types() {
    if ( ! bp_is_user() ) {
    return;
    }

    $user_id = bp_displayed_user_id();

    if ( ! bp_get_member_type( $user_id, ‘agent’ ) ) {
    bp_core_remove_nav_item( ‘groups’ );
    }

    }

    add_action( ‘bp_setup_nav’, ‘buddydev_remove_group_based_on_member_types’, 1001 );
    /**
    * Remove groups from adminbar based on member type.
    *
    * @param array $nav nav items.
    *
    * @return array
    */
    function buddydev_filter_show_hide_groups_in_adminbar( $nav ) {
    if ( ! is_user_logged_in() ) {
    return $nav;
    }

    if ( ! bp_get_member_type( bp_loggedin_user_id(), ‘agent’ ) ) {
    $nav = array();
    }

    return $nav;
    }

    add_filter( ‘bp_groups_admin_nav’, ‘buddydev_filter_show_hide_groups_in_adminbar’ );

    Regards,
    Beuza

  • Keymaster
    (BuddyDev Team)
    Posts: 21463
    Brajesh Singh on #11809

    Hi Beuza,
    May I see how have you registered the member types? You need to use the same type.
    I have tested the code and it works with my example above, so I am guessing the issue is with name.

    Can you please post the code that you are using for registering member type?

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 38
    Beuza on #11817
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 21463
    Brajesh Singh on #11819
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 38
    Beuza on #11820
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 38
    Beuza on #11841
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 38
    Beuza on #11843

    I just saw that Buddydev have its Member type Pro plugin too. I buy it because of your prompt support ability to help a non client.

    Best regards,

    Beuza

  • Keymaster
    (BuddyDev Team)
    Posts: 21463
    Brajesh Singh on #11849

    Hi Laurent,
    Thank you.
    It was a kind gesture by you but not required.

    I am going to install and use the other plugin and check the code. I will get back to you within an hour with the details.

    Thank you
    Brajesh

The topic ‘ [Resolved] Profile tabs depending to user role or member type’ is closed to new replies.

This topic is: resolved