Let Editors Manage Menus and Widgets
Gives editors access to Appearance > Menus and Widgets without letting them switch themes or open the Customizer.
Opens WPIDE on
Code
add_filter( 'user_has_cap', function ( $allcaps, $caps, $args, $user ) {
if ( in_array( 'editor', (array) $user->roles, true ) ) {
$allcaps['edit_theme_options'] = true;
}
return $allcaps;
}, 10, 4 );
// No Customizer for users who cannot switch themes.
add_filter( 'map_meta_cap', function ( $caps, $cap, $user_id ) {
if ( 'customize' === $cap && ! user_can( $user_id, 'switch_themes' ) ) {
$caps[] = 'do_not_allow';
}
return $caps;
}, 10, 3 );
add_action( 'admin_menu', function () {
global $submenu;
if ( current_user_can( 'switch_themes' ) || empty( $submenu['themes.php'] ) ) {
return;
}
foreach ( $submenu['themes.php'] as $item ) {
if ( ! in_array( $item[2], array( 'nav-menus.php', 'widgets.php' ), true ) ) {
remove_submenu_page( 'themes.php', $item[2] );
}
}
}, 999 );
add_action( 'load-themes.php', function () {
if ( ! current_user_can( 'switch_themes' ) ) {
wp_safe_redirect( admin_url( 'nav-menus.php' ) );
exit;
}
} );
Notes
On block themes, menus live in the Site Editor, which editors can also open with this capability.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.