Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am creating a theme that deletes some user roles that are not necessary and creates others. (It deletes author and contributor and renames subscriber).

Everything works, but on theme deactivation, I want to readd all the roles I deleted. Do I have to manually go in and create the roles (author and contributor), manually adding in all the capabilities listed here: http://codex.wordpress.org/Roles_and_Capabilities ?

There are two reasons I don't want to do this:

  1. More code.
  2. If capabilities and roles get updated, on every Wordpress update I'll have to manually check I'm adding the right capabilities back.

There must be a better solution for undoing all role changes I made on deactivation. Right?

share|improve this question
2  
What code are you using when deactivation occurs? :::: IMO, Reason 1 is not reasonable... –  brasofilo Jan 20 '13 at 15:12
2  
There's no reason to add roles to Themes. This is functionality that belongs to a plugin and nowhere else. Also you shouldn't delete roles, just add your own ones and use capability mapping (or go with default capabilities - even better). –  kaiser Jan 20 '13 at 15:58
    
I take your point. I'm also developing a plugin alongside the theme. I'll do the role changes there now. Where you see theme read plugin. –  The Salt Jan 25 '13 at 16:55
add comment

2 Answers

up vote 1 down vote accepted

Use a plugin to manage roles, not a theme. Roles are not for presentation.

In your plugin use register_activation_hook() and register_deactivation_hook() to add or remove new roles. Do not remove built-in roles, other plugins may rely on their existence.

share|improve this answer
    
Okay, I've accepted the answer, which is what I feared: default roles should not be deleted so there should be no need to create the defaults back. –  The Salt Jan 25 '13 at 16:53
add comment

The following code will reset a WordPress default role to it's default capabilities. Note: these defaults are for WordPress 3.5, any other versions might have different defaults. To be safe, only use this code on WordPress 3.5.

Warning

This cannot be undone! The role, in it's current state, is removed and recreated with it's default values.

/*
 * example usage: $results = reset_role_wpse_82378( 'subscriber' );
 * per add_role() (WordPress Codex):
 * $results "Returns a WP_Role object on success, null if that role already exists."
 *
 * possible $role values:
 * 'administrator'
 * 'editor'
 * 'author'
 * 'contributor'
 * 'subscriber'
 */
function reset_role_wpse_82378( $role ) {
    $default_roles = array(
        'administrator' => array(
            'switch_themes' => 1,
            'edit_themes' => 1,
            'activate_plugins' => 1,
            'edit_plugins' => 1,
            'edit_users' => 1,
            'edit_files' => 1,
            'manage_options' => 1,
            'moderate_comments' => 1,
            'manage_categories' => 1,
            'manage_links' => 1,
            'upload_files' => 1,
            'import' => 1,
            'unfiltered_html' => 1,
            'edit_posts' => 1,
            'edit_others_posts' => 1,
            'edit_published_posts' => 1,
            'publish_posts' => 1,
            'edit_pages' => 1,
            'read' => 1,
            'level_10' => 1,
            'level_9' => 1,
            'level_8' => 1,
            'level_7' => 1,
            'level_6' => 1,
            'level_5' => 1,
            'level_4' => 1,
            'level_3' => 1,
            'level_2' => 1,
            'level_1' => 1,
            'level_0' => 1,
            'edit_others_pages' => 1,
            'edit_published_pages' => 1,
            'publish_pages' => 1,
            'delete_pages' => 1,
            'delete_others_pages' => 1,
            'delete_published_pages' => 1,
            'delete_posts' => 1,
            'delete_others_posts' => 1,
            'delete_published_posts' => 1,
            'delete_private_posts' => 1,
            'edit_private_posts' => 1,
            'read_private_posts' => 1,
            'delete_private_pages' => 1,
            'edit_private_pages' => 1,
            'read_private_pages' => 1,
            'delete_users' => 1,
            'create_users' => 1,
            'unfiltered_upload' => 1,
            'edit_dashboard' => 1,
            'update_plugins' => 1,
            'delete_plugins' => 1,
            'install_plugins' => 1,
            'update_themes' => 1,
            'install_themes' => 1,
            'update_core' => 1,
            'list_users' => 1,
            'remove_users' => 1,
            'add_users' => 1,
            'promote_users' => 1,
            'edit_theme_options' => 1,
            'delete_themes' => 1,
            'export' => 1,
        ),
        'editor' => array(
            'moderate_comments' => 1,
            'manage_categories' => 1,
            'manage_links' => 1,
            'upload_files' => 1,
            'unfiltered_html' => 1,
            'edit_posts' => 1,
            'edit_others_posts' => 1,
            'edit_published_posts' => 1,
            'publish_posts' => 1,
            'edit_pages' => 1,
            'read' => 1,
            'level_7' => 1,
            'level_6' => 1,
            'level_5' => 1,
            'level_4' => 1,
            'level_3' => 1,
            'level_2' => 1,
            'level_1' => 1,
            'level_0' => 1,
            'edit_others_pages' => 1,
            'edit_published_pages' => 1,
            'publish_pages' => 1,
            'delete_pages' => 1,
            'delete_others_pages' => 1,
            'delete_published_pages' => 1,
            'delete_posts' => 1,
            'delete_others_posts' => 1,
            'delete_published_posts' => 1,
            'delete_private_posts' => 1,
            'edit_private_posts' => 1,
            'read_private_posts' => 1,
            'delete_private_pages' => 1,
            'edit_private_pages' => 1,
            'read_private_pages' => 1,
        ),
        'author' => array(
            'upload_files' => 1,
            'edit_posts' => 1,
            'edit_published_posts' => 1,
            'publish_posts' => 1,
            'read' => 1,
            'level_2' => 1,
            'level_1' => 1,
            'level_0' => 1,
            'delete_posts' => 1,
            'delete_published_posts' => 1,
        ),
        'contributor' => array(
            'edit_posts' => 1,
            'read' => 1,
            'level_1' => 1,
            'level_0' => 1,
            'delete_posts' => 1,
        ),
        'subscriber' => array(
            'read' => 1,
            'level_0' => 1,
        ),
        'display_name' => array(
            'administrator' => 'Administrator',
            'editor'        => 'Editor',
            'author'        => 'Author',
            'contributor'   => 'Contributor',
            'subscriber'    => 'Subscriber',
        ),
    );
    $role = strtolower( $role );
    remove_role( $role );
    return add_role( $role, $default_roles['display_name'][$role], $default_roles[$role] );
} // function reset_role_wpse_82378
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.