admin管理员组

文章数量:1130349

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.

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.

本文标签: Removing Admin Bar Node Based on Role