Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to Hide Certain Menu Items WordPress

  • SOLVED

I have a site where I would like to hide certain menu items from anyone except admin. I found this amazing piece of code here that does the trick...

// HIDE MENUS EXCEPT ADMIN
function remove_menus()
{
global $menu;
global $current_user;
get_currentuserinfo();

if($current_user->user_login != 'admin')
{
$restricted = array(__('Posts'),
__('Media'),
__('Links'),
__('Pages'),
__('Comments'),
__('Appearance'),
__('Plugins'),
__('Users'),
__('Tools'),
__('Settings')
);
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}// end while

}// end if
}
add_action('admin_menu', 'remove_menus');


However, I have 4 other menu items that I'm not sure how to hide: Slides (part of WooTheme), Polls and Ratings (from PollDaddy plugin), and Forms (which is from Gravity Forms plugin). Here's the screenshot of them - [[LINK href="http://cl.ly/343H2A2j2z070i2j3S0F"]]http://cl.ly/343H2A2j2z070i2j3S0F[[/LINK]]. I tried adding one of them to the above code, like this...

__('Forms'),

But that didn't work.

Would anyone be able to help, and to know where in the code I could find how to hide these 4 menu items?

Thanks for your help!

Answers (2)

2011-07-14

Utkarsh Kukreti answers:

Please add

var_dump($menu);

after global $menu;

and paste the output.

Code:

function remove_menus()
{
global $menu;
global $current_user;
get_currentuserinfo();

if($current_user->user_login != 'spencerb')
{
$restricted = array(__('Posts'),
__('Media'),
__('Links'),
__('Pages'),
__('Comments'),
__('Appearance'),
__('Plugins'),
__('Users'),
__('Tools'),
__('Slide'),
__('Forms'),
__('Polls'),
__('Ratings'),
__('Settings')

);

foreach($menu as $key => $value) {
$value = explode(" ", $value[0]);
$value = $value[0];
if(in_array($value, $restricted)) {
unset($menu[$key]);
}
}


}// end if

}

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


Spencer Barfuss comments:

array(18) { [2]=> array(7) { [0]=> string(9) "Dashboard" [1]=> string(4) "read" [2]=> string(9) "index.php" [3]=> string(0) "" [4]=> string(43) "menu-top menu-top-first menu-icon-dashboard" [5]=> string(14) "menu-dashboard" [6]=> string(3) "div" } [4]=> array(5) { [0]=> string(0) "" [1]=> string(4) "read" [2]=> string(10) "separator1" [3]=> string(0) "" [4]=> string(17) "wp-menu-separator" } [5]=> array(7) { [0]=> string(5) "Posts" [1]=> string(10) "edit_posts" [2]=> string(8) "edit.php" [3]=> string(0) "" [4]=> string(37) "open-if-no-js menu-top menu-icon-post" [5]=> string(10) "menu-posts" [6]=> string(3) "div" } [10]=> array(7) { [0]=> string(5) "Media" [1]=> string(12) "upload_files" [2]=> string(10) "upload.php" [3]=> string(0) "" [4]=> string(24) "menu-top menu-icon-media" [5]=> string(10) "menu-media" [6]=> string(3) "div" } [15]=> array(7) { [0]=> string(5) "Links" [1]=> string(12) "manage_links" [2]=> string(16) "link-manager.php" [3]=> string(0) "" [4]=> string(24) "menu-top menu-icon-links" [5]=> string(10) "menu-links" [6]=> string(3) "div" } [20]=> array(7) { [0]=> string(5) "Pages" [1]=> string(10) "edit_pages" [2]=> string(23) "edit.php?post_type=page" [3]=> string(0) "" [4]=> string(23) "menu-top menu-icon-page" [5]=> string(10) "menu-pages" [6]=> string(3) "div" } [25]=> array(7) { [0]=> string(87) "Comments 0" [1]=> string(10) "edit_posts" [2]=> string(17) "edit-comments.php" [3]=> string(0) "" [4]=> string(27) "menu-top menu-icon-comments" [5]=> string(13) "menu-comments" [6]=> string(3) "div" } [26]=> array(7) { [0]=> string(6) "Slides" [1]=> string(10) "edit_posts" [2]=> string(24) "edit.php?post_type=slide" [3]=> string(0) "" [4]=> string(24) "menu-top menu-icon-slide" [5]=> string(16) "menu-posts-slide" [6]=> string(72) "http://pefriends.com/wp-content/themes/canvas/includes/images/slides.png" } [59]=> array(5) { [0]=> string(0) "" [1]=> string(4) "read" [2]=> string(10) "separator2" [3]=> string(0) "" [4]=> string(17) "wp-menu-separator" } [60]=> array(7) { [0]=> string(10) "Appearance" [1]=> string(18) "edit_theme_options" [2]=> string(10) "themes.php" [3]=> string(0) "" [4]=> string(29) "menu-top menu-icon-appearance" [5]=> string(15) "menu-appearance" [6]=> string(3) "div" } [65]=> array(7) { [0]=> string(8) "Plugins " [1]=> string(16) "activate_plugins" [2]=> string(11) "plugins.php" [3]=> string(0) "" [4]=> string(26) "menu-top menu-icon-plugins" [5]=> string(12) "menu-plugins" [6]=> string(3) "div" } [70]=> array(7) { [0]=> string(7) "Profile" [1]=> string(4) "read" [2]=> string(11) "profile.php" [3]=> string(0) "" [4]=> string(24) "menu-top menu-icon-users" [5]=> string(10) "menu-users" [6]=> string(3) "div" } [75]=> array(7) { [0]=> string(5) "Tools" [1]=> string(10) "edit_posts" [2]=> string(9) "tools.php" [3]=> string(0) "" [4]=> string(24) "menu-top menu-icon-tools" [5]=> string(10) "menu-tools" [6]=> string(3) "div" } [80]=> array(7) { [0]=> string(8) "Settings" [1]=> string(14) "manage_options" [2]=> string(19) "options-general.php" [3]=> string(0) "" [4]=> string(27) "menu-top menu-icon-settings" [5]=> string(13) "menu-settings" [6]=> string(3) "div" } [99]=> array(5) { [0]=> string(0) "" [1]=> string(4) "read" [2]=> string(14) "separator-last" [3]=> string(0) "" [4]=> string(17) "wp-menu-separator" } [100]=> array(7) { [0]=> string(13) "Smart Sharing" [1]=> string(13) "administrator" [2]=> string(30) "smartsharing/smart-sharing.php" [3]=> string(22) "Smart Sharing Settings" [4]=> string(49) "menu-top toplevel_page_smartsharing/smart-sharing" [5]=> string(40) "toplevel_page_smartsharing/smart-sharing" [6]=> string(68) "http://pefriends.com/wp-content/plugins/smartsharing/images/icon.png" } [101]=> array(7) { [0]=> string(11) "Performance" [1]=> string(14) "manage_options" [2]=> string(12) "w3tc_general" [3]=> string(11) "Performance" [4]=> string(35) "menu-top toplevel_page_w3tc_general" [5]=> string(26) "toplevel_page_w3tc_general" [6]=> string(80) "http://pefriends.com/wp-content/plugins/w3-total-cache/inc/images/logo_small.png" } [27]=> array(7) { [0]=> string(6) "Canvas" [1]=> string(14) "manage_options" [2]=> string(9) "woothemes" [3]=> string(10) "Page Title" [4]=> string(32) "menu-top toplevel_page_woothemes" [5]=> string(23) "toplevel_page_woothemes" [6]=> string(75) "http://pefriends.com/wp-content/themes/canvas/functions/images/woo-icon.png" } }


Utkarsh Kukreti comments:

Change the add_action to

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

This should solve 'Forms', and probably the other two too.


Spencer Barfuss comments:

Added that, but they are still showing.


Utkarsh Kukreti comments:

Could you post the complete code you used?


Spencer Barfuss comments:

// HIDE MENUS EXCEPT ADMIN
function remove_menus()
{
global $menu;
global $current_user;
get_currentuserinfo();

if($current_user->user_login != 'spencerb')
{
$restricted = array(__('Posts'),
__('Media'),
__('Links'),
__('Pages'),
__('Comments'),
__('Appearance'),
__('Plugins'),
__('Users'),
__('Tools'),
__('Slide'),
__('Settings')
);
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}// end while

}// end if
}
add_action('admin_menu', 'remove_menus', 11);


Spencer Barfuss comments:

Sorry, here it is...

// HIDE MENUS EXCEPT ADMIN
function remove_menus()
{
global $menu;
global $current_user;
get_currentuserinfo();

if($current_user->user_login != 'admin')
{
$restricted = array(__('Posts'),
__('Media'),
__('Links'),
__('Pages'),
__('Comments'),
__('Appearance'),
__('Plugins'),
__('Users'),
__('Tools'),
__('Settings')
);
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}// end while

}// end if
}
add_action('admin_menu', 'remove_menus', 11);


Utkarsh Kukreti comments:

Please try the code I posted above. It works for PollDaddy and Gravity Forms for me. You'll need to add the correct title of the Slides menu.


Spencer Barfuss comments:

Hmmm... still not working for me. Logged out and then logged back in, but still showing on the left.


Spencer Barfuss comments:

Ok, almost there. I added those few lines of code that you appended to your original reply, and all I have left showing is Slides and Ratings, and that's it.

Any idea on how to find the names of those? I've tried using 'Slides' and 'Ratings', but it doesn't seem to work.

Thanks for your help!


Utkarsh Kukreti comments:

Try "Slides" instead of __("Slides"). If that doesn't work, try increasing 11 to 1000 and see if it works.


Spencer Barfuss comments:

Ok, changed it to 'Slide' (singular), instead of 'Slides', and it worked! Now all that remains is 'Ratings'. I just tried 'Rating' (singular), and that didn't work either. I'm going to add the 1000 to it.


Utkarsh Kukreti comments:

Is that Ratings from the PollDaddy plugin? If so, it works for me.


Spencer Barfuss comments:

Yes, it is for the Poll Daddy plugin.

2011-07-14

Romel Apuya answers:

Hi,

u try adding remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
before }// end if
you can also refer to this article [[LINK href="http://codex.wordpress.org/Dashboard_Widgets_API#Advanced:_Removing_Dashboard_Widgets"]]Removing Items on dashboard[[/LINK]]


Spencer Barfuss comments:

Hmmm... that gave me an error. Can you show me where you would paste your code snippet using the code that's been pasted above?


Romel Apuya comments:

here


function remove_menus()

{

global $menu;

global $current_user;

get_currentuserinfo();



if($current_user->user_login != 'admin')

{

$restricted = array(__('Posts'),

__('Media'),

__('Links'),

__('Pages'),

__('Comments'),

__('Appearance'),

__('Plugins'),

__('Users'),

__('Tools'),

__('Settings')

);

end ($menu);

while (prev($menu)){

$value = explode(' ',$menu[key($menu)][0]);

if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}

}// end while


remove_meta_box( 'dashboard_poll_daddy', 'dashboard', 'side' );
}// end if

}


Romel Apuya comments:

sorry :

function remove_menus()

{

global $menu;

global $current_user;

get_currentuserinfo();



if($current_user->user_login != 'admin')

{

$restricted = array(__('Posts'),

__('Media'),

__('Links'),

__('Pages'),

__('Comments'),

__('Appearance'),

__('Plugins'),

__('Users'),

__('Tools'),

__('Settings')

);

end ($menu);

while (prev($menu)){

$value = explode(' ',$menu[key($menu)][0]);

if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}

}// end while


remove_meta_box( 'dashboard_poll_daddy', 'dashboard', 'side' );
}// end if

}


Spencer Barfuss comments:

Unfortunately, that did not work.


Romel Apuya comments:

try replacing poll_daddy with the polls poll daddy id?


Spencer Barfuss comments:

The only one left showing is the Ratings meta box on the side. It is with the Poll Daddy plugin, but does your code include the Ratings box as well?