How to change in customizer the "site identity" tab required capabilities

I'm building a multisite in which site admins have no "manage options" capability. The problem is that withut this cap they cannot see the "site identity" tab in the customizer, where they can change the site icon (favicon) as well as the site's name.

Is there a way to change the required capability to show this tab and save its contents? If not, is there a way to display the same fields as in this tab but in a custom tab in the customizer?

Thank you and have a nice day.

Topic theme-customizer favicon capabilities customization Wordpress

Category Web


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 );

You can remove the title_tagline section from the customizer and add your own section with customs control, which are basicly a copy of "Site Identity" piece here.

You will only have to change the capability specified there with one that your administrators have.

You can check out how to use WordPress Customize API Manager in the official doc.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.