Skip to content

Instantly share code, notes, and snippets.

@numediaweb
Last active October 25, 2022 22:46
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save numediaweb/7dc94a428d0bc9d175b1 to your computer and use it in GitHub Desktop.
Save numediaweb/7dc94a428d0bc9d175b1 to your computer and use it in GitHub Desktop.
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');
Copy link

ghost commented Apr 16, 2015

Excellent solution, thank you so much!

@Orangedrop
Copy link

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

@jaredrethman
Copy link

Great function - great share!

@devner007
Copy link

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