Skip to content

Instantly share code, notes, and snippets.

@frankiejarrett
Last active December 15, 2015 07:38
Show Gist options
  • Save frankiejarrett/5224398 to your computer and use it in GitHub Desktop.
Save frankiejarrett/5224398 to your computer and use it in GitHub Desktop.
Restricts certain menu items from appearing the WordPress Admin area. Useful for hiding unused features such as Posts or Comments from non-Administrator users.
<?php
/**
* Restricts certain menu items from appearing the WP Admin area. Does not
* affect Administrator users.
*
* @action admin_menu
*/
function fjarrett_restrict_admin_menu_items() {
// Don't restrict Administrator users.
if ( current_user_can( 'manage_options' ) )
return;
// Array of the menu item slugs you want to remove.
$restricted = array(
'menu-posts', // Posts
'menu-comments', // Comments
);
global $menu;
foreach ( $menu as $item => $data ) {
if ( ! isset( $data[5] ) ) {
continue; // Move along if the current $item doesn't have a slug.
} elseif ( in_array( $data[5], $restricted ) ) {
unset( $menu[$item] ); // Remove the current $item from the $menu.
}
}
}
add_action( 'admin_menu', 'fjarrett_restrict_admin_menu_items' );
@frankiejarrett
Copy link
Author

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