最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Removing Admin Bar Node Based on Role

matteradmin10PV0评论

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' );
}

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' );
}
Share Improve this question asked Nov 15, 2016 at 14:43 Taylor FosterTaylor Foster 1812 silver badges4 bronze badges 1
  • Any luck with this? – jgraup Commented Nov 19, 2016 at 17:31
Add a comment  | 

2 Answers 2

Reset to default 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' );
    }
}

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far