BuddyDev

Search

Hide nav menu item for certain user role(s)

  • Participant
    Level: Initiated
    Posts: 16
    Chanel on #52252

    Hi there,

    I’ve been looking for a way to hide a menu item from users with the “subscriber” role and came across a code snippet but it’s not working as intended. Could someone please guide me in the right direction? I would like to do it without a plugin as I have too many installed on my site already. Is anyone here familiar with this function?

     function bp_remove_nav_item() {
        $current_user   = wp_get_current_user();
        $role_name      = $current_user->roles[0];
        if($role_name==='administrator'){
           bp_core_remove_nav_item( 'buddyblog' );
        }
    }
    add_action( 'wp', 'bp_remove_nav_item' );
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #52257

    Hello Chanel,

    Thank you for posting. I am assuming you are not using the BuddyBlog Pro plugin. Please try the following code:

    
    add_action( 'bp_buddyblog_setup_nav', function () {
    
    	$user_id = bp_displayed_user_id();
    	$user    = $user_id ? get_user_by( 'id', $user_id ) : null;
    
    	if ( $user && array_intersect( (array) $user->roles, array( 'administrator' ) ) ) {
    		bp_core_remove_nav_item( 'buddyblog', 'members' );
    	}
    }, 15 );
    
    

    It will remove the user nav menu

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 16
    Chanel on #52263

    Hi Ravi,

    I’m using buddyboss and don’t have buddyblog pro installed. Sadley, the code snippet you provided did not help me achieve my desired result as the menu item is still showing up in the header navigation where I would like to place it. Do you have any other suggestions?

    Best regards,
    Chanel

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #52267

    Hello Chanel,

    Sorry, It didn’t work for you. Please share the screenshot from where you want to remove it. I checked it with the user navigation menu and it works fine there.

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: not resolved