Last active
October 25, 2022 22:46
-
-
Save numediaweb/7dc94a428d0bc9d175b1 to your computer and use it in GitHub Desktop.
Filter WordPress admin side navigation menues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
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
Great function - great share!