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
2 Answers
Reset to default 0WP_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
2 Answers
Reset to default 0WP_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
版权声明:本文标题:Removing Admin Bar Node Based on Role 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749236414a2337349.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论