Skip to content

Instantly share code, notes, and snippets.

@rtpHarry
Forked from felixzapata/gist:9144986
Last active October 14, 2022 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtpHarry/6e1d73d026fca369569f11798736dc7a to your computer and use it in GitHub Desktop.
Save rtpHarry/6e1d73d026fca369569f11798736dc7a to your computer and use it in GitHub Desktop.
How to create a clone role in WordPress
<?php
/**
* Clone the subscriber role to create a tenant role
* Note: this only needs to be run once, so it can be removed after the role is created
*/
function clone_subscriber_to_tenant() {
$new_role = 'tenant';
$new_role_name = 'Tenant';
$source_role = 'subscriber';
$wp_roles = wp_roles();
// skip if role exists
if ($wp_roles->get_role($new_role)) {
return;
}
$source_capabilities = $wp_roles->get_role($source_role);
if (!$source_capabilities) {
return;
}
$wp_roles->add_role($new_role, $new_role_name, $source_capabilities->capabilities);
}
add_action('init', 'clone_subscriber_to_tenant');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment