BuddyDev

Search

[Resolved] Hide Upper Right Menu at top bar

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #4026

    Hi there,
    Thanks so much for this site and I’m very fond of this upgraded new version. Well done!

    I just wonder that is there any way to hide upper right menu, including submenu?

    For example, I want to hide Profile -> Edit and Change Avatar, I tried use CSS – display:none; to make them invisible but it seems like the selector #wp-admin-bar-my-account-xprofile-edit doesn’t work.

    Could you find some solutions for me please? Thanks a lot.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #4029

    Hi Dandy,

    Welcome back again.I will get back to you by tomorrow.

    Thank You
    Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #4048

    Hello Dandy,

    You can hide the Admin Bar Menu by following ways.

    1. By putting the following code in your current theme’s style.css file.

    
    #wp-admin-bar-my-account-xprofile-edit,
    #wp-admin-bar-my-account-xprofile-change-cover-image {
    	display:none !important;
    }
    
    

    2. By putting the following code in your bp-custom.php file

    
    function buddydev_remove_unwanted_menu() {
    
        global $wp_admin_bar;
    
        // place the node id you want to hide
        $node_ids = array(
            'my-account-xprofile-edit',
            'my-account-xprofile-change-avatar',
            'my-account-xprofile-change-cover-image'
        );
    
        foreach( $node_ids as $id ) {
            $wp_admin_bar->remove_node( $id );
        }
    
    }
    add_action( 'wp_before_admin_bar_render', 'buddydev_remove_unwanted_menu' );
    
    

    Please let me know know if It is works for you or not.

    Thank You
    Ravi

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #4055

    Hi, Ravi
    Thanks for your help and code snippet. It works perfectly.

    However, I just wonder that is there any way to hide these items based on specific user roles? For example, if I only want to Role A to see these two items, but I don’t want Role B to see them, what can I do for that?

    Thanks.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #4056

    Hi Dandy,

    Yes, it can be done to hide and show these items based on user role or capabilities. Please take a look the following url for WordPress User role and capability.

    https://codex.wordpress.org/Roles_and_Capabilities

    
    function buddydev_remove_unwanted_menu() {
    
        global $wp_admin_bar;
    	
    	/**
    	 * 
    	 *  either you can pass the capability or the user role like
    	 *  current_user_can('edit_posts') with user capability
    	 *  current_user_can('author') with user role
    	 * 
    	**/
    	
    	if ( current_user_can('edit_posts') ) { 
    		// place the node id you want to hide
    		$node_ids = array(
    			'my-account-xprofile-edit',
    			'my-account-xprofile-change-avatar',
    			'my-account-xprofile-change-cover-image'
    		);
    
    		foreach( $node_ids as $id ) {
    			$wp_admin_bar->remove_node( $id );
    		}
    	}
    	
    }
    add_action( 'wp_before_admin_bar_render', 'buddydev_remove_unwanted_menu' );
    
    

    Please let me know if it works for you or not.

    Thank You
    Ravi

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #4058

    Hi, Ravi
    Thanks for reply. I tried it but it looks can’t work and return HTTP 500 Error…

    Thanks.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #4059

    Hi Dandy,

    Can You please share the code block that you are using?. I have tested this code on my local server and It is working for me.

    Thank You
    Ravi

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #4060

    Hi, Ravi
    Sure.
    function buddydev_remove_unwanted_menu() {

    global $wp_admin_bar;

    /**
    *
    * either you can pass the capability or the user role like
    * current_user_can(‘edit_posts’) with user capability
    * current_user_can(‘author’) with user role
    *
    **/

    if ( current_user_can(‘teacher’) || current_user_can(‘principal’) ) {
    //In my site, I want to hide teacher and principal’s edit and change avatar submenu
    // place the node id you want to hide
    $node_ids = array(
    ‘my-account-xprofile-edit’,
    ‘my-account-xprofile-change-avatar’,
    ‘my-account-xprofile-change-cover-image’
    );

    foreach( $node_ids as $id ) {
    $wp_admin_bar->remove_node( $id );
    }
    }
    }
    add_action( ‘wp_before_admin_bar_render’, ‘buddydev_remove_unwanted_menu’ );

    Thanks.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #4061

    Hi Dandy,

    There is problem with your code ‘current_user_can’ is a WordPress function and works only with WordPress Roles and Capabilities. Please try the following code

    
    
    function buddydev_remove_unwanted_menu() {
    
    	if( ! is_user_logged_in() || ! function_exists( 'buddypress' ) ) {
    		return;
    	}
    
    	global $wp_admin_bar;
    
    	$is_teacher = bp_has_member_type( bp_loggedin_user_id(), 'teacher' );
    	$is_principal = bp_has_member_type( bp_loggedin_user_id(), 'principal' );
    
    	if ( $is_teacher || $is_principal ) {
    		// place the node id you want to hide
    		$node_ids = array(
    			'my-account-xprofile-edit',
    			'my-account-xprofile-change-avatar',
    			'my-account-xprofile-change-cover-image'
    		);
    
    		foreach( $node_ids as $id ) {
    			$wp_admin_bar->remove_node( $id );
    		}
    	}
    
    }
    add_action( 'wp_before_admin_bar_render', 'buddydev_remove_unwanted_menu' );
    
    
    • This reply was modified 7 years, 10 months ago by Ravi.
    • This reply was modified 7 years, 10 months ago by Ravi.
  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #4065

    Hi, Ravi

    Thanks for updated reply. I tried it but unfortunately, it still doesn’t work. I’m using a plugin to create the teacher and principal’s user role, it’s User Role Editor Pro, does it make any difference with default WordPress user role?

    Thanks.

You must be logged in to reply to this topic.

This topic is: resolved