0

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

1
  • Try using Adminimize plugin. It's simple to use and powerful
    – David
    Commented Feb 9, 2020 at 21:38

2 Answers 2

0

First 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.

1
  • Can you please read again? I said page hide not working but post_type is working.
    – Shawn
    Commented Feb 11, 2020 at 3:07
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.