7

I'm trying to hide certain admin menu items in Wordpress from all users except one (myself).

I can find various tutorials but they mostly hide on user roles rather than users.

I have found this from the Wordpress codex:

<?php 
function custom_menu_page_removing() {
    remove_menu_page( $menu_slug );
}
add_action( 'admin_menu', 'custom_menu_page_removing' );
?>

But I don't fully understand it, plus I think I'll need to add some more PHP to it to essentially make the code say:

If user isn't shaun@seedcreativity.co.uk (Me!)

Then remove these menu items:

ItemID 1, ItemID 2, ItemID 3, etc...

Can anyone help?

4 Answers 4

13

You can check for the user id:

// admin_init action works better than admin_menu in modern wordpress (at least v5+)
add_action( 'admin_init', 'my_remove_menu_pages' );
function my_remove_menu_pages() {


  global $user_ID;

  if ( $user_ID != 1 ) { //your user id

   remove_menu_page('edit.php'); // Posts
   remove_menu_page('upload.php'); // Media
   remove_menu_page('link-manager.php'); // Links
   remove_menu_page('edit-comments.php'); // Comments
   remove_menu_page('edit.php?post_type=page'); // Pages
   remove_menu_page('plugins.php'); // Plugins
   remove_menu_page('themes.php'); // Appearance
   remove_menu_page('users.php'); // Users
   remove_menu_page('tools.php'); // Tools
   remove_menu_page('options-general.php'); // Settings
  }
}
1
  • Great: admin_menu won't work in current wp version, while admin_init works perfect Jan 11, 2022 at 10:02
11

You can remove Posts menu for given e-mail with:

function custom_menu_page_removing() {
    if ( get_currentuserinfo()->user_email != 'shaun@seedcreativity.co.uk' )
        remove_menu_page( 'edit.php' );
}
add_action( 'admin_menu', 'custom_menu_page_removing' );

It will not prevent users from using removed pages if they can guess proper URL e.g. /wp-admin/edit.php

0
6

You can try this code.

function remove_menus(){
     $current_user = wp_get_current_user();
    if( 'shaun@seedcreativity.co.uk' !==  $current_user->user_email){
        remove_menu_page( 'item1' ); 
        remove_menu_page( 'item2' ); 
    }

}
add_action( 'admin_menu', 'remove_menus' );

item1, item2 will your page name for example http://test.com/wp-admin/admin.php?page=item1 http://test.com/wp-admin/admin.php?page=item2

3
  • Thats Perfect, thank you :) If I wanted to add a second user, other than myself, how would I add that? I tried adding: . 'new email' after mine, but it didn't work... Is it s simple add on like that? Mar 21, 2017 at 17:45
  • function remove_menus(){ $current_user = wp_get_current_user(); if( in_array( $current_user->user_email, array('firstuser@email.com','seconduser@email.com',)) ){ remove_menu_page( 'item1' ); remove_menu_page( 'item2' ); } } add_action( 'admin_menu', 'remove_menus' ); use it like this for multiple users Apr 13, 2017 at 10:00
  • you can use this plugin. wordpress.org/plugins/hide-admin-menu Jan 3, 2019 at 6:20
0

** To hide woocommerce, marketing, acf fields and analytics menu use below code**

add_action( 'admin_init', 'remove_menu_pages' );
function remove_menu_pages() {


  global $user_ID;

  if ( $user_ID != 1 ) { 
   remove_menu_page( 'edit.php?post_type=acf-field-group' );
   remove_menu_page( 'edit.php?post_type=product' );
   remove_menu_page('woocommerce');
   remove_menu_page('wc-admin&path=/analytics/overview');
   remove_menu_page('woocommerce-marketing');
  }
}
1
  • It is not acceptable to copy and paste the answer that was generated by AI. This is a form of exploitation of the developer, who worked hard to provide the answer. It is important to respect the hard work and effort that goes into creating and maintaining AI systems, and to use them responsibly. Instead of simply copying and pasting the answer, try to understand the solution and explain it in your own words, or give credit to the source of the information. @user58006.
    – DSDmark
    Dec 20, 2022 at 13:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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