Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top