This is how I've interpreted from the wordpress docs at least. Originally these settings were made with add_setting
and it is where the capability was originally set. Fortunately, we can use get_setting
to change that value. It seems to work very well for your case.
function wpseo_206907_add_back_customizer_controls_for_not_admins( $wp_customize ) {
$wp_customize->get_setting( 'blogname' )->capability = 'edit_theme_options'; // or edit_posts or whatever capability your site owner has
}
add_action( 'customize_register', 'wpseo_206907_add_back_customizer_controls_for_not_admins', 1000 );
If for some reason they don't have access to the customizer you need to give them the edit_theme_options capability first.
function wpseo_206951_add_capability_for_non_admin() {
$roleObject = get_role( 'editor' ); // whoever should have access to theme changes
if (!$roleObject->has_cap( 'edit_theme_options' ) ) {
$roleObject->add_cap( 'edit_theme_options' );
}
}
add_action( 'admin_init', 'wpseo_206951_add_capability_for_non_admin');
This will give them access to the following:
Appearance > Widgets
Appearance > Menus
Appearance > Customize if they are supported by the current theme
Appearance > Background
Appearance > Header
If you'd rather hide these pages all together do this:
function wpseo_206907_remove_by_caps_admin_menu() {
if ( !current_user_can('manage_options') ) {
remove_menu_page('themes.php'); // Appearance Menu on Admin
remove_submenu_page( 'themes.php', 'widgets.php' );
remove_submenu_page( 'themes.php', 'nav-menus.php' );
remove_submenu_page( 'themes.php', 'theme-editor.php' );
}
}
add_action('admin_menu', 'wpseo_206907_remove_by_caps_admin_menu', 999);
However, if you want them to have access to certain pages like widgets and menus, but not themes then do this instead:
add_action( 'admin_init', 'wpseo_206907_lock_theme' );
function wpseo_206907_lock_theme() {
global $submenu, $userdata;
get_currentuserinfo();
if ( $userdata->ID != 1 ) {
unset( $submenu['themes.php'][5] );
unset( $submenu['themes.php'][15] );
}
}
You'd also want to do this then to remove the theme change section section from the customizer:
function wpseo_206951_remove_customizer_controls_all( $wp_customize ) {
if ( !current_user_can('manage_options') ) {
$wp_customize->remove_section("themes"); // Removes Themes section from backend
// To remove other sections, panels, controls look in html source code in chrome dev tools or firefox or whatever and it will tell you the id and whether it's a section or panel or control.
//Example below (uncomment to use)
// $wp_customize->remove_section("title_tagline");
// $wp_customize->remove_panel("nav_menus");
// $wp_customize->remove_panel("widgets");
// $wp_customize->remove_section("static_front_page");
}
}
add_action( 'customize_register', 'wpseo_206951_remove_customizer_controls_all', 999 );