How to hide nodes on admin tool bar by role?

We have a multisite and BuddyPress community site. The site admins are only subscribers on the community site - so the only node that I want visible on the Community node is visit. I am also using User Role Editor Pro which I can block permissions to the admin bar but three nodes: dashboard, new post and manage comments are still visible. If a subscriber clicks on them they get the no permission but what I would like is to hide these three nodes by role. Any suggestions?

  • Jude

    Howdy antKat , Hope you are keeping well !

    This will require a combination of these two snippets shown here

    https://digwp.com/2011/04/admin-bar-tricks/#disable-for-non-admins
    https://digwp.com/2011/04/admin-bar-tricks/#add-remove-links

    You basically can add / edit the roles for which the nodes are going to be visible.

    Insert that into either your theme's functions.php, or you can insert it as a mu-plugin.

    To create an mu-plugin, go to your /wp-content/ folder via FTP, and find the folder called "mu-plugins". If there is no folder with that name, then you'll want to create one. Then, create a file inside that folder, and give the file any name you like (making sure the file as an extension of .php, e.g. example.php) and paste the code in there.

    You don't need to activate that plugin, as it will be always be automatically activated. Please be sure to add a PHP start tag at the beginning of the code if you insert this code as a mu-plugin, like this:

    <?php

    Thank you for being a WPMU DEV member and have a fantastic day!

    Cheers
    Jude

  • Michael Bissett

    Hey antKat, Michael here!

    To clarify, what my colleague Jude was advising was something like this:

    function mytheme_admin_bar_render() {
    	global $wp_admin_bar;
    	if (!current_user_can('manage_options')) {
    		$wp_admin_bar->remove_menu('comments');
    		$wp_admin_bar->remove_menu('new-content');
    	}
    }
    add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );

    When you say that you want to remove the "dashboard" link, are you referring to this:

    If you are, then you'll want to change the code to be like this:

    function mytheme_admin_bar_render() {
    	global $wp_admin_bar;
    	if (!current_user_can('manage_options')) {
    		$wp_admin_bar->remove_menu('comments');
    		$wp_admin_bar->remove_menu('new-content');
    		$wp_admin_bar->remove_menu('dashboard');
    	}
    }
    add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );

    If not, then could you please provide a screenshot of the menu item in question? Having that, as well as Support Access to the site in question (what's the site address?), would be very helpful. :slight_smile:

    Kind Regards,
    Michael

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.