Skip to content

Instantly share code, notes, and snippets.

@mdpatrick
Created April 12, 2012 16:34
Show Gist options
  • Save mdpatrick/2368918 to your computer and use it in GitHub Desktop.
Save mdpatrick/2368918 to your computer and use it in GitHub Desktop.
Disabling WordPress Menus for Certain User(s)
/**
/* Add to functions.php
/* In this case the user has an id of "2"
*/
function remove_menu_items() {
if (!(wp_get_current_user()->id == 2))
return;
global $menu;
$restricted = array(__('Links'), __('Comments'), __('Media'),
__('Plugins'), __('Tools'), __('Users'), __('Settings'), __('Appearance'), __('Options'));
foreach ($menu as $key => $category) {
foreach ($restricted as $restrictedName) {
if ((substr_count($category[0], $restrictedName) > 0)) { unset($menu[$key]); }
}
}
}
add_action('admin_menu', 'remove_menu_items');
@mdpatrick
Copy link
Author

This implementation is a rip of http://www.wprecipes.com/how-to-remove-menus-in-wordpress-dashboard ... but it fixes two bugs: disabling wasnt working on menu names lacking spaces, nor was it working if one of the restricted menus happened to be the last in the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment