Skip to content

How-to Guides

Technical References

Customize user roles

Sometimes the default roles and capabilities aren’t exactly what you need for your site. If you need to create new roles or modify existing ones, we have helper functions for WordPress.com and for VIP Go to assist you in doing this. Please use these functions rather than the traditional methods as this will ensure that your code works on WordPress.com and in your development environments.

As an example, here’s how you can register a “Reviewer” role:

add_action( 'init', function() {
    $ver = 42; // bump each time this code is changed
    // check if this has been run already
    if ( $ver <= get_option( 'custom_roles_version' ) ) {
        return;
    }
 
    // add a Reviewer role
    wpcom_vip_add_role( 'reviewer', 'Reviewer', array(
        'read' => true,
        'edit_posts' => true,
        'edit_others_posts' => true,
        'edit_private_posts' => true,
        'edit_published_posts' => true,
        'read_private_posts' => true,
        'edit_pages' => true,
        'edit_others_pages' => true,
        'edit_private_pages' => true,
        'edit_published_pages' => true,
        'read_private_pages' => true,
        )
    );
 
    // update the version to prevent this running again
    update_option( 'custom_roles_version', $ver );
} );

Note

You’ll want to use these helper functions on the ‘init’ hook, and ensure you only run them when the role definitions need to change, because they trigger a database UPDATE. An example technique is shown that triggers only when a version changes.

⚠️ If your site has a high amount of traffic, then we’d highly recommend restricting this type of update to is_admin() or put the update behind an admin button, on a special settings page, or create a CLI command to trigger the roles update. That will avoid a sudden spike of multiple identical DB updates that can be triggered when a change like this is made during a high traffic period. Such spikes can cause performance issues.

You can find all available capabilities in WordPress Handbook.

Here are some more examples:

add_action( 'init', function() {
    $ver = 43; // bump each time this code is changed
    // check if this has been run already
    if ( $ver <= get_option( 'custom_roles_version' ) {
        return;
    }
     
    // Add new role
    wpcom_vip_add_role( 'super-editor', 'Super Editor', array( 'level_0' => true ) );
 
    // Remove publish_posts cap from authors
    wpcom_vip_merge_role_caps( 'author', array( 'publish_posts' => false ) );
 
    // Remove all caps from contributors
    wpcom_vip_override_role_caps( 'contributor', array( 'level_0' => false ) );
 
    // Duplicate an existing role and modify some caps
    wpcom_vip_duplicate_role( 'administrator', 'station-administrator', 'Station Administrator',
        array( 'manage_categories' => false ) );
 
    // Add custom cap to a role
    wpcom_vip_add_role_caps( 'administrator', array( 'my-custom-cap' ) );
 
    // Remove cap from a role
    wpcom_vip_remove_role_caps( 'author', array( 'publish_posts' ) );
 
    // update the version to prevent this running again
    update_option( 'custom_roles_version', $ver );
} );

Last updated: November 20, 2020