Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding admin menu items in Wordpress

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 [email protected] (Me!)

Then remove these menu items:

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

Can anyone help?

like image 375
Shaun Taylor Avatar asked Mar 21 '17 17:03

Shaun Taylor


1 Answers

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
  }
}
like image 193
AfikDeri Avatar answered Sep 20 '22 18:09

AfikDeri