0

I have a custom user that i'm trying to minimize access for. I have a few admin menus in the toolbar that I'd like removed but only for this customer user role. I currently have it working for everyone that isn't an admin but I can't get the if function correct by user role. My current code:

//Remove admin menu items if not admin
function remove_admin_bar_links() {
    global $wp_admin_bar, $current_user;

    if ($current_user->ID != 1) {
        $wp_admin_bar->remove_menu('updates');          // Remove the updates link
        $wp_admin_bar->remove_menu('comments');         // Remove the comments link
        $wp_admin_bar->remove_menu('new-content');      // Remove the content link
        $wp_admin_bar->remove_menu('wp-logo');          // Remove the WP Logo link
        $wp_admin_bar->remove_menu('wpseo-menu');       // Remove the Yoast SEO menu
    }
}
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );
2

You can use wp_get_current_user() function to get current logged in user's role

//Remove admin menu items if not admin

function remove_admin_bar_links() {
    global $wp_admin_bar, $current_user;

     $user = wp_get_current_user();
     if ( in_array( 'author', (array) $user->roles ) ) {
         $wp_admin_bar->remove_menu('updates');          // Remove the updates link
         $wp_admin_bar->remove_menu('comments');         // Remove the comments link
         $wp_admin_bar->remove_menu('new-content');      // Remove the content link
         $wp_admin_bar->remove_menu('wp-logo');          // Remove the WP Logo link
         $wp_admin_bar->remove_menu('wpseo-menu');       // Remove the Yoast SEO menu
     }
 }
 add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );

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.