0

I have a custom role, grocery, that I would like to remove the "new content" (the + New dropdown) node from the top admin bar while this role is logged in. I have the following function but it is removing it currently for all roles including admin. Would like to find a way to limit this to just 'grocery' custom role.

functions.php

add_action( 'admin_bar_menu', 'remove_new_content_menu', 999 );

function remove_new_content_menu( $wp_admin_bar ) {
    $wp_admin_bar->remove_node( 'new-content' );
}
1
  • Any luck with this?
    – jgraup
    Nov 19 '16 at 17:31
0

WP_User has a roles array.

Get the current user with wp_get_current_user() and check if your role is in the array.


add_action( 'admin_bar_menu', 'remove_new_content_menu', PHP_INT_MAX );

function remove_new_content_menu( $wp_admin_bar ) {

    // get the current user
    $user = wp_get_current_user();

    // define roles that cannot see the `new content` button
    $blacklisted_roles = array('grocery', 'subscriber');

    // remove the button if the current user has a blacklisted role
    if( array_intersect($blacklisted_roles, $user->roles ) ) {
        $wp_admin_bar->remove_node( 'new-content' );
    }
}
0

You just need to check the role of the current user

add_action( 'admin_bar_menu', 'remove_new_content_menu', 999 );

function remove_new_content_menu( $wp_admin_bar ) {
   $user = wp_get_current_user();
   if ( $user && in_array( 'grocery', (array) $user->roles ) ) {
        $wp_admin_bar->remove_node( 'new-content' );
    }
}

Although a better option may be to check for a particular capability as role names may change.

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.