Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Filter WordPress admin side navigation menues
function filter_admin_menues() {
// If administrator then do nothing
if (current_user_can('activate_plugins')) return;
// Remove main menus
$main_menus_to_stay = array(
// Dashboard
'index.php',
// Edit
'edit.php',
// Media
'upload.php'
);
// Remove sub menus
$sub_menus_to_stay = array(
// Dashboard
'index.php' => ['index.php'],
// Edit
'edit.php' => ['edit.php', 'post-new.php'],
// Media
'upload.php' => ['upload.php', 'media-new.php'],
);
if (isset($GLOBALS['menu']) && is_array($GLOBALS['menu'])) {
foreach ($GLOBALS['menu'] as $k => $main_menu_array) {
// Remove main menu
if (!in_array($main_menu_array[2], $main_menus_to_stay)) {
remove_menu_page($main_menu_array[2]);
} else {
// Remove submenu
foreach ($GLOBALS['submenu'][$main_menu_array[2]] as $k => $sub_menu_array) {
if (!in_array($sub_menu_array[2], $sub_menus_to_stay[$main_menu_array[2]])) {
remove_submenu_page($main_menu_array[2], $sub_menu_array[2]);
}
}
}
}
}
}
// Filter admin side navigation menues
add_action('admin_init', 'filter_admin_menues');
@numediaweb

This comment has been minimized.

Copy link
Owner Author

numediaweb commented Jan 2, 2015

Enjoy it :)

@ghost

This comment has been minimized.

Copy link

ghost commented Apr 16, 2015

Excellent solution, thank you so much!

@Orangedrop

This comment has been minimized.

Copy link

Orangedrop commented Feb 18, 2016

This is awesome..... Thank you so much for sharing !! ;)

@jaredrethman

This comment has been minimized.

Copy link

jaredrethman commented May 25, 2016

Great function - great share!

@devner007

This comment has been minimized.

Copy link

devner007 commented Jul 14, 2019

AWESOME CODE! Works perfectly! Thanks a ton for sharing!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.