admin管理员组文章数量:1390736
I want to hide page menu from the admin panel for a specific user type. I tried it with the below codes in
functions.php
function hide_menu_items() {
global $menu;
global $current_user;
get_currentuserinfo();
if( $current_user->user_login == 'username' ):
remove_menu_page( 'admin.php?page=megamenu' );
remove_menu_page( 'admin.php?page=mycustompage' );
endif;
}
add_action( 'admin_menu', 'hide_menu_items' );
It's not working but it only hides post_types
I want to hide page menu from the admin panel for a specific user type. I tried it with the below codes in
functions.php
function hide_menu_items() {
global $menu;
global $current_user;
get_currentuserinfo();
if( $current_user->user_login == 'username' ):
remove_menu_page( 'admin.php?page=megamenu' );
remove_menu_page( 'admin.php?page=mycustompage' );
endif;
}
add_action( 'admin_menu', 'hide_menu_items' );
It's not working but it only hides post_types
Share Improve this question edited Feb 9, 2020 at 11:11 Shawn asked Feb 9, 2020 at 10:58 ShawnShawn 11710 bronze badges 1- Try using Adminimize plugin. It's simple to use and powerful – David Commented Feb 9, 2020 at 21:38
2 Answers
Reset to default 0First of all, as you can see in the documentation, the function
<?php
// is deprecated
get_currentuserinfo();
has been deprecated. So, you should not build new projects with it.
Also, you are not declaring a variable for the function get_currentuserinfo, meaning that the result of that function is floating around somewhere in the air, not declared to a variable.
You should rather try:
<?php
function hide_menu_items() {
$user = wp_get_current_user();
if(current_user_can('editor')) {
//The user has the "editor" role
remove_menu_page( 'edit.php?post_type=page' );
}
}
add_action( 'admin_menu', 'hide_menu_items' );
?>
I have tested the above code and it works (removes the "pages" from the admin menu). You have to adjust the url of the remove_menu_page function to your needs.
<?php
// can check for capability and role
current_user_can('something');
?>
Usually expects a capability (e.g. 'edit_posts') but can also accept a role like "editor" or a custom role.
I fix my problem using below codes:
function hide_admin_menu()
{
global $menu;
global $current_user;
get_currentuserinfo();
if ( $current_user->user_login == 'username' ) {
remove_menu_page( 'megamenu' );
remove_menu_page( 'mycustompage' );
}
}
add_action('admin_menu', 'hide_admin_menu', 999);
Just add page name to remove the page from specific users
本文标签: hide page menu from admin panel for specific users
版权声明:本文标题:hide page menu from admin panel for specific users 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744743643a2622758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论