Skip to content

Instantly share code, notes, and snippets.

@lordspace
Last active February 14, 2021 16:46
Show Gist options
  • Save lordspace/8688dd990cbba152ba3dcf017f6874dd to your computer and use it in GitHub Desktop.
Save lordspace/8688dd990cbba152ba3dcf017f6874dd to your computer and use it in GitHub Desktop.
How to Allow Non-Admin Users to Access WordPress Customizer. Check the latest version on the qSandbox - https://qsandbox.com/675
<?php
/**
* How to Allow Non-Admin Users to Access WordPress Customizer. Published on the qSandbox - https://qsandbox.com/675
* <a href="https://qsandbox.com/?utm_source=qs_blog_code&utm_medium=p675" title="Free Test/Staging WordPress site by qSandbox.com">Free Test/Staging WordPress site by qSandbox.com</a>
* @param array $all_caps
* @param array $cap
* @param array $args
* @return array array
* @see https://codex.wordpress.org/Plugin_API/Filter_Reference/user_has_cap
* @see https://wordpress.stackexchange.com/questions/12984/how-can-i-allow-the-editor-role-to-change-theme-settings
* @see https://codex.wordpress.org/Class_Reference/WP_User#has_cap.28.24cap.29
*/
function qs_blog675_grant_customizer_access( $all_caps, $cap = [], $args = [] ) {
remove_filter( 'user_has_cap', __METHOD__, 25, 3 ); // Prevent loop if other code calls this method.
$cap = 'edit_others_posts';
// Editor but without access to customizer
if ( current_user_can( $cap ) ) {
$all_caps['customize'] = true; // since WP 4.0
$all_caps['edit_theme_options'] = true; // do we need this?
}
add_filter( 'user_has_cap', __METHOD__, 25, 3 ); // Re-add the call the action is consistent.
return $all_caps;
}
add_filter( 'user_has_cap', 'qs_blog675_grant_customizer_access', 25, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment