Shaun Taylor
Shaun Taylor

Reputation: 972

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?

Upvotes: 7

Views: 33416

Answers (4)

user58006
user58006

Reputation: 1

** 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');
  }
}

Upvotes: 0

AfikDeri
AfikDeri

Reputation: 2109

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
  }
}

Upvotes: 16

Naveen Giri
Naveen Giri

Reputation: 545

You can try this code.

function remove_menus(){
     $current_user = wp_get_current_user();
    if( '[email protected]' !==  $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

Upvotes: 7

Krzyc
Krzyc

Reputation: 397

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

function custom_menu_page_removing() {
    if ( get_currentuserinfo()->user_email != '[email protected]' )
        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

Upvotes: 12

Related Questions