How to dynamically allow editor to access customizer on multisite?

Background

For reasons I don’t want to permanently grant the required capabilities to editor users to allow them to access the Customizer. So I’m using user_has_cap filter to set capabilities for editors dynamically.

I also don’t want to categorically grant the dynamic capabilities, but restrict them to certain admin views.

So this is not what I want to do,

add_filter(‘user_has_cap’, function($allcaps, $caps, $args, $user) {
// check if user is editor
$allcaps[‘edit_theme_options’] = true;
return $allcaps;
}, 11, 4);

I can use the global $pagenow to grant capabilities where I want. However this does not work for customize.php as the view has some ajax actions, which cause the $pagenow to be admin-ajax.php and not customize.php.

add_filter(‘user_has_cap’, function($allcaps, $caps, $args, $user) {
// check if user is editor
// check if global $pagenow is customize.php
$allcaps[‘edit_theme_options’] = true;
return $allcaps;
}, 11, 4);

To circumenvent the trouble above with ajax, I set the required capabilities for customize.php view on customize_loaded_components hook. Might not be the cleanest way, but it gets the job done.

add_filter(‘customize_loaded_components’, function($components, $customize){
$user = wp_get_current_user();
if ( in_array( ‘editor’, $user->roles ) ) {
$user->allcaps[‘edit_theme_options’] = true;
$user->allcaps[‘customize’] = true;
}
return $components;
}, 10, 2);

Problem

The above code works great on a single WordPress install. User with editor role can access the Customizer and do stuff. However the code doesn’t work on multisite environment, where wild do_not_allows appear messing the whole thing up. Or at least I believe it is the do_not_allows that are causing the problem.

Question

Any suggestions what would be the magic combination for map_meta_cap and user_has_cap filters to remove do_not_allow’s and to make the above code work and to dynamically allow editors to access the Customizer in multisite?

I’m starting to run in circles and just can’t get this working.

Or is there something else I’m missing?

Read more here:: How to dynamically allow editor to access customizer on multisite?

Leave a Reply

Your email address will not be published. Required fields are marked *