Skip to content

Instantly share code, notes, and snippets.

@hellofromtonya
Created June 19, 2018 16:21
Show Gist options
  • Save hellofromtonya/3565f09abb48bad91eda9e7bdd80c689 to your computer and use it in GitHub Desktop.
Save hellofromtonya/3565f09abb48bad91eda9e7bdd80c689 to your computer and use it in GitHub Desktop.
Removes target menu items from the admin menus.
add_action( 'admin_menu', 'remove_breakable_links_from_admin', 9999 );
/**
* Remove the "breakable" links from the admin menus. These are the links deemed to break the site unless you know
* what you're doing.
*
* @since 1.0.0
*
* @return void
*/
function remove_breakable_links_from_admin() {
global $submenu;
// Loop through the configuration of items to be removed.
foreach ( _get_config_admin_submenu_items_to_remove() as $page => $to_remove ) {
// Nope, this submenu page doesn't exist for this user. Let's skip it.
if ( ! isset( $submenu[ $page ] ) ) {
continue;
}
// Remove this page's menu items.
_remove_submenu_items_from_admin_page( $page, $to_remove );
}
}
/**
* Get the configuration for the admin submenu items to be removed.
*
* @since 1.0.0
*
* @return array
*/
function _get_config_admin_submenu_items_to_remove() {
return array(
/*=============================================================
* Format is:
*
* page => array of items to be removed
*
* Typically, you use the submenu slug for a specific item to remove.
* Why not the menu name? Internationalization. The name will change,
* but the slug stays the same.
*
* For certain menu items, such as the Settings menu, the slug is not
* available in the $submenu global. Darn. So instead, we use the link.
*============================================================*/
// On the themes.php admin page, use the submenu slug.
'themes.php' => array( 'customize', 'edit_themes' ),
// On the Settings page, use the PHP link as the slug is not given in $submenu.
'options-general.php' => array( 'options-discussions.php', 'options-permalink.php' ),
);
}
/**
* Remove the given menu items from the given submenu page.
*
* @since 1.0.0
* @ignore
* @access private
*
* @param string $page The given submenu page.
* @param array $menu_items_to_remove Menu items to remove.
*
* @return void
*/
function _remove_submenu_items_from_admin_page( $page, array $menu_items_to_remove ) {
global $submenu;
foreach ( $submenu[ $page ] as $index => $menu_item ) {
// Loop through the items to be removed.
foreach ( $menu_items_to_remove as $to_remove_item ) {
// Nope, the item to be removed isn't in this menu item. Let's skip it.
if ( ! in_array( $to_remove_item, $menu_item, true ) ) {
continue;
}
// Remove this menu item.
unset( $submenu[ $page ][ $index ] );
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment