Reputation: 124
I am removing all items that I do not want on the sidebar and I thought it would be easier to tell wordpress what I want to show up instead of what I dont want to show up so that if things get added in the future they wont show up on the side bar by default.
I have added this code and everything is showing up like it should except for posts and the logout link that I have at the bottom. Can anyone help me figure this out? I know I am using the correct slug so Im not sure why its not working.
add_action('admin_init', 'nwcm_admin_init');
function nwcm_admin_init()
{
if (!current_user_can('editor')) {
return;
}
$menus_to_stay = array(
'index.php',
'edit.php',
'upload.php',
'edit.php?post_type=page',
'nav-menus.php',
'post-new.php',
'admin.php?page=logout'
);
foreach ($GLOBALS['menu'] as $key => $value) {
if (!in_array($value[2], $menus_to_stay))
remove_menu_page($value[2]);
}
}
I need to do this in functions.php and not with a plugin so please dont recommend plugins!
Upvotes: 0
Views: 87
Reputation: 644
Your action is not correct, use admin_menu
https://codex.wordpress.org/Function_Reference/remove_menu_page
add_action('admin_menu', 'nwcm_admin_init');
function nwcm_admin_init()
{
if (!current_user_can('editor'))
return;
$menus_to_stay = array(
'index.php',
'edit.php',
'upload.php',
'edit.php?post_type=page',
'nav-menus.php',
'post-new.php',
'admin.php?page=logout'
);
foreach ($GLOBALS['menu'] as $key => $value) {
if (!in_array($value[2], $menus_to_stay))
remove_menu_page($value[2]);
}
}
Upvotes: 1