Loading...

OK here’s a couple of useful bits of code which will help you identify the items in your WordPress dashboard menu, then remove them for certain user roles.

First up lets see what is in the menu for a Roots\Sage Framework site: Don’t forget to add in the ‘pre’ tags around the print_r!

function debug_admin_menu() {
    echo '<div style="position:fixed; z-index:9999; right:0; width:66%; top:0; bottom:0; background:#ffffff; overflow-y:scroll; padding: 30px 25px;">' . print_r( $GLOBALS[ 'menu' ], TRUE) . '</div>';
}
add_action( 'admin_init',  __NAMESPACE__ . '\\debug_admin_menu' );

Or maybe you’re still stuck in the past and haven’t moved up to Roots\Sage yet in which case this goes in your theme functions file:

function nnm_23012018_debug_admin_menu() {
    echo '<div style="position:fixed; z-index:9999; right:0; width:66%; top:0; bottom:0; background:#ffffff; overflow-y:scroll; padding: 30px 25px;">' . print_r( $GLOBALS[ 'menu' ], TRUE) . '</div>';
}
add_action( 'admin_init',  'nnm_23012018_debug_admin_menu' );

Now when you load the dashboard, the Menu globals are printed out there.
The info we need is in position [2] of each array, known as ‘the slug‘, and now we know the values of these slugs we can tell the menu to ignore them, like so in extras.php (for Roots\Sage):

/**
 * Remove dashboard menu items
 */
function admin_menu_cleanup(){
	$items = array(
		'seperator1',
		'edit.php',
		'upload.php',
		'edit.php?post_type=page',
		'edit-comments.php',
		'wpcf7',
		'edit.php?post_type=product',
		'themes.php',
		'plugins.php',
		'tools.php',
		'options-general.php',
		'admin.php?page=social-menu',
	);
	if(current_user_can('subscriber')) {
		for ($i=0; $i < count($items); $i++) { 
			remove_menu_page($items[$i]);		
		}		
	}
}
add_action('admin_init', __NAMESPACE__ . '\\admin_menu_cleanup');

Boom! Now all those items are removed for the Subscriber users. You can use the $_GLOBALS['menu'] array to keep the ones you specify and remove all the others if you're so inclined. All you need to do is loop through the array check the slug [2] and perform the remove_menu_page action for the ones that don't match your pre-approved list.

Also try global $submenu; and print_r($submenu); in the first function to see the Sub Menu information.

Latest Posts

Portfolio

Contact

Fill in the form below to send us a message. Your details will not be used for promotional purposes or passed on to any third parties.

 

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close