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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744743644a4591177.html
评论列表(0条)