重置角色(或在主题更改时撤消角色更改)

时间:2013-01-20 作者:The Salt

我正在创建一个主题,删除一些不必要的用户角色,并创建其他角色。(它删除作者和贡献者,并重命名订阅者)。

一切正常,但在主题停用时,我想读取我删除的所有角色。我是否必须手动进入并创建角色(作者和贡献者),手动添加此处列出的所有功能:http://codex.wordpress.org/Roles_and_Capabilities ?

我不想这样做有两个原因:

更多代码必须有更好的解决方案来撤消我在停用时所做的所有角色更改。正当

2 个回复
最合适的回答,由SO网友:fuxia 整理而成

使用插件管理角色,not a theme. 角色不用于演示。

在插件使用中register_activation_hook()register_deactivation_hook() 添加或删除新角色。不要删除内置角色,其他插件可能依赖于它们的存在。

SO网友:akTed

以下代码将WordPress默认角色重置为其默认功能。注意:这些默认值适用于WordPress 3.5,其他版本可能有不同的默认值。为了安全起见,only use this code on WordPress 3.5.

警告:此操作无法撤消!角色在其当前状态下将被删除并使用其默认值重新创建。

/*
 * 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

结束