如何为执行特定任务的自定义帖子类型创建新权限

时间:2017-06-27 作者:Subrata Sarkar

我需要创建具有以下访问权限的新权限:

Post\\u Type\\u A(自定义Post Type):访问Post\\u Type\\u A内容类型
Post\\u Type\\u B(自定义Post Type):访问Post\\u Type\\u B内容类型

我是WordPress的新手,不知道从哪里开始。如果有人能指导我,我将不胜感激。

当做

1 个回复
SO网友:CodeMascot

检查以下代码-

add_action( \'admin_init\', \'the_dramatist_add_role_caps\', 999 );
function the_dramatist_add_role_caps() {
    // Add the roles you\'d like to administer the custom post types
    $roles = array( \'editor\', \'administrator\' );

    // Loop through each role and assign capabilities
    foreach($roles as $the_role) {

        $role = get_role($the_role);

        $role->add_cap( \'read\' );
        $role->add_cap( \'read_{Post_Type_A_Name}\' ); // Also do for Post_Type_B
        $role->add_cap( \'read_private_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
        $role->add_cap( \'edit_{Post_Type_A_Name}\' ); // Also do for Post_Type_B
        $role->add_cap( \'edit_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
        $role->add_cap( \'edit_others_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
        $role->add_cap( \'edit_published_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
        $role->add_cap( \'publish_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
        $role->add_cap( \'delete_others_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
        $role->add_cap( \'delete_private_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
        $role->add_cap( \'delete_published_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B

    }
}
通过这种方式,您可以向任何用户角色添加权限。

如果要为应获得权限的用户创建新角色,请创建如下新角色-

function add_the_dramatist_role() {
    add_role(\'the_dramatist_role\',
             \'The Dramatist Role\',
             array(
                 \'read\' => true,
                 \'edit_posts\' => false,
                 \'delete_posts\' => false,
                 \'publish_posts\' => false,
                 \'upload_files\' => true,
             )
    );
}
// Remember this must be called plugin main file.
register_activation_hook( __FILE__, \'add_the_dramatist_role\' );
然后添加the_dramatist_role 到上一个函数the_dramatist_add_role_caps\'s$roles 像这样的变量-

$roles = array( \'the_dramatist_role\', \'editor\', \'administrator\' );
现在the_dramatist_role 将拥有所有这些特权。

希望以上帮助。

结束

相关推荐

Recommended File Permissions

嘿,伙计们,我花了很长时间试图解决这个问题。我想知道WordPress中的文件权限应该是什么样子in order to use the autoupdate feature. 到目前为止,我的wordpress安装程序一直在询问我的FTP信息,我不想使用那种升级/安装方法,我想使用纯/直接PHP。某些上下文:Web服务器和php fcgi守护程序运行为www-data:www-data</wordpress安装位于/home/blaenk/sites/domain.tld/</首先,我read