Skip to content

Instantly share code, notes, and snippets.

@gutenpack
Forked from ControlledChaos/README.md
Created September 23, 2017 14:55
Show Gist options
  • Save gutenpack/04ddc00cbdf99b824f207448da4b814a to your computer and use it in GitHub Desktop.
Save gutenpack/04ddc00cbdf99b824f207448da4b814a 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