Skip to content

Instantly share code, notes, and snippets.

@ControlledChaos
Last active January 9, 2019 15:47
Show Gist options
  • Save ControlledChaos/74eddd6fab785570debf0cf55c082a4b to your computer and use it in GitHub Desktop.
Save ControlledChaos/74eddd6fab785570debf0cf55c082a4b to your computer and use it in GitHub Desktop.
Turns the WordPress Menus and Widgets links in the admin menu into top-level menu items.

Move Menus & Widgets

WordPress Snippet

<?php
function ccd_move_menu_items() {
global $submenu, $menu;
if ( isset( $submenu['themes.php'] ) ) {
foreach ( $submenu['themes.php'] as $key => $item ) {
if ( $item[2] === 'nav-menus.php' ) {
unset($submenu['themes.php'][$key] );
}
if ( $item[2] === 'widgets.php' ) {
unset( $submenu['themes.php'][$key] );
}
}
}
$user = wp_get_current_user();
if ( in_array( 'editor', $user->roles ) ) {
unset( $menu[60] );
}
add_menu_page(
__( 'Menus' ),
__( 'Menus' ),
'delete_others_pages',
'nav-menus.php',
'',
'dashicons-list-view',
61
);
add_menu_page(
__( 'Widgets' ),
__( 'Widgets' ),
'delete_others_pages',
'widgets.php',
'',
'dashicons-welcome-widgets-menus',
62
);
}
add_action( 'admin_menu', 'ccd_move_menu_items' );
function ccd_menu_parent_file( $parent_file ) {
global $current_screen;
if ( $current_screen->base == 'nav-menus' ) {
$parent_file = 'nav-menus.php';
}
if ( $current_screen->base == 'widgets' ) {
$parent_file = 'widgets.php';
}
return $parent_file;
}
add_filter( 'parent_file', 'ccd_menu_parent_file' );
function ccd_has_cap( $caps, $cap, $args, $user ) {
$url = $_SERVER['REQUEST_URI'];
if ( strpos( $url, 'nav-menus.php' ) !== false && in_array( 'edit_theme_options', $cap ) && in_array( 'editor', $user->roles ) ) {
$caps['edit_theme_options'] = true;
}
if ( strpos( $url, 'widgets.php' ) !== false && in_array( 'edit_theme_options', $cap ) && in_array( 'editor', $user->roles ) ) {
$caps['edit_theme_options'] = true;
}
return $caps;
}
add_filter( 'user_has_cap', 'ccd_has_cap', 20, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment