In an effort to make a better boilerplate my co-worker Jim Floss and I set out to create a new User Role inside our WordPress client sites. This role closely resembles the Editor role and Admin role but allows us to turn different functionality on a per-client basis if need be, but by default locks them out of many things such as updates and plugins. I didn’t see much information on cloning a role and removing functionality online so I figured I would put it up here! Enjoy.
/**
* Adds a 'Client' user role
* https://codex.wordpress.org/Roles_and_Capabilities
*/
add_action('init', 'add_client_role');
function add_client_role() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
//Adding a 'new_role' with all admin caps
$wp_roles->add_role('client', 'Client', $adm->capabilities);
}
add_action('init', 'revoke_client_capabilities', 10);
function revoke_client_capabilities() {
$caps_to_remove = array(
'update_core',
'activate_plugins',
'install_plugins',
'update_plugin',
'edit_plugins',
'edit_themes',
'export',
'import',
'create_users',
'manage_options',
'switch_themes' // etc
);
$custom_role = get_role('client'); // Edit according to your role as it was declared when added
foreach($caps_to_remove as $cap) {
$custom_role->remove_cap($cap);
}
}
Leave a Reply