Created
March 19, 2021 15:47
-
-
Save MattMcAdams/2eeebe8dd537130d9f4e4cc5403223d8 to your computer and use it in GitHub Desktop.
Give WP editors access to edit menus and only menus
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
// Allow editors to see access the Menus page under Appearance but hide other options | |
function hide_menu() { | |
$user = wp_get_current_user(); | |
// Check if the current user is an Editor | |
if ( in_array( 'editor', (array) $user->roles ) ) { | |
// They're an editor, so grant the edit_theme_options capability if they don't have it | |
if ( ! current_user_can( 'edit_theme_options' ) ) { | |
$role_object = get_role( 'editor' ); | |
$role_object->add_cap( 'edit_theme_options' ); | |
} | |
// Hide the Themes page | |
remove_submenu_page( 'themes.php', 'themes.php' ); | |
// Hide the Widgets page | |
remove_submenu_page( 'themes.php', 'widgets.php' ); | |
// Hide the Customize page | |
remove_submenu_page( 'themes.php', 'customize.php' ); | |
// Remove Customize from the Appearance submenu | |
global $submenu; | |
unset($submenu['themes.php'][6]); | |
} | |
} | |
// Remove customize from the admin bar | |
function remove_customize() { | |
global $wp_admin_bar; | |
$wp_admin_bar->remove_menu('customize'); | |
} | |
// Remove the additional CSS section | |
function remove_css_section( $wp_customize ) { | |
$wp_customize->remove_section( 'custom_css' ); | |
} | |
$user = wp_get_current_user(); | |
if ( in_array( 'editor', (array) $user->roles ) ) { | |
add_action('admin_menu', 'hide_menu', 10); | |
add_action('wp_before_admin_bar_render', 'remove_customize'); | |
add_action( 'customize_register', 'remove_css_section', 15 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment