0

How do I make it so that a menu on the WP Admin menu bar is only visible to specified user roles?

I have this code which works to remove WP Admin menu items from all users. Is there any way to customize it so that only certain user roles can view these menu item? Thank you!

function shapeSpace_remove_toolbar_menu() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('site-name');
}
add_action('wp_before_admin_bar_render', 'shapeSpace_remove_toolbar_menu', 999);
  • if ( ! current_user_can('manage_options') ) { $wp_admin_bar->remove_menu( 'site-name' ); } will display the Site name menu item to only the administrator users. – Mayeenul Islam Mar 8 at 4:13
1
function shapeSpace_remove_toolbar_menu() {
    global $wp_admin_bar;
    // remove menu for editor and author        
    if( current_user_can( 'editor' ) || current_user_can( 'author' )  ){
         $wp_admin_bar->remove_menu('site-name');
       }
 }
add_action('wp_before_admin_bar_render', 'shapeSpace_remove_toolbar_menu', 999);

All Roles:

current_user_can( 'administrator' )
current_user_can( 'editor' )
current_user_can( 'author' )
current_user_can( 'contributor' )
current_user_can( 'subscriber' )
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.