Creating custom user roles

时间:2012-08-22 作者:RRikesh

我需要创建一个自定义用户角色,该角色将能够创建帖子(特定帖子类型),但不能发布帖子(就像当前contributor role.

我对如何创建新的rolecapability 为了实现这一点。如何/从哪里开始?

谢谢

UPDATE:我按照Eric Holmes的建议,将这些添加到自定义post函数中

\'capabilities\' => array(
    \'edit_posts\' => \'edit_helps\',
    \'edit_post\' => \'edit_help\',
    \'read_post\' => \'read_helps\',
),
将这些添加到插件激活挂钩,在停用挂钩中添加了相反的内容(我正在修改参与者角色本身):

function modify_user_capabilities() {
  $role = get_role( \'contributor\' );
  $role->remove_cap( \'delete_posts\' );
  $role->remove_cap( \'edit_posts\' );
  $role->add_cap(\'edit_helps\');
  $role->add_cap(\'edit_help\');
  $role->add_cap(\'read_helps\');
  }

register_activation_hook( __FILE__, \'modify_user_capabilities\' );
现在只有投稿人可以编辑此帖子类型,其他用户(如管理员)不能编辑此帖子类型

是否有更好的方法批量分配这些功能?我将激活挂钩编辑为:

function modify_user_capabilities() {
  $role = get_role( \'contributor\' );
  $role->remove_cap( \'delete_posts\' );
  $role->remove_cap( \'edit_posts\' );

  foreach (array(\'administrator\', \'editor\', \'author\', \'contributor\') as $user_role) {
    $role = get_role($user_role);
    $role->add_cap(\'edit_helps\');
    $role->add_cap(\'edit_help\');
    $role->add_cap(\'read_helps\');
  }  
}
UPDATE 2:我完全忘了指定人删除帖子。所以我更新了函数:

function modify_user_capabilities() {

  //remove the contributor from editing any post
  $role = get_role( \'contributor\' );
  $role->remove_cap( \'delete_posts\' );
  $role->remove_cap( \'edit_posts\' );

  foreach (array(\'administrator\', \'editor\', \'author\', \'contributor\') as $user_role) {
    $role = get_role($user_role);
    $role->add_cap(\'edit_helps\');
    $role->add_cap(\'edit_help\');
    $role->add_cap(\'read_helps\');
  }
  //let admins delete posts
  $role = get_role(\'administrator\');
  $role->add_cap(\'delete_helps\');
  $role->add_cap(\'delete_publised_helps\');
  $role->add_cap(\'delete_others_helps\');
  $role->add_cap(\'delete_help\');
}
现在管理员可以删除这些帖子。

1 个回复
最合适的回答,由SO网友:Eric Holmes 整理而成

添加role 非常简单。创建自定义功能更让人头疼。当你register your custom post type, 您可以定义您的it能力。从本质上讲,它是一个数组“你想把它算什么?”。我下面的例子将澄清这一说法。

$caps = array(
    \'edit_post\' => \'edit_cpt_name\',
    \'edit_posts\' => \'edit_cpt_names\',
    \'manage_posts\' => \'manage_cpt_names\',
    \'publish_posts\' => \'publish_cpt_names\',
    \'edit_others_posts\' => \'edit_others_cpt_names\',
    \'delete_posts\' => \'delete_cpt_names\'
);
因此,很明显,您会用自定义帖子类型的slug(或任何您真正想要的东西)替换“cpt\\u name”。左侧的项目是默认的功能名称(还有更多,请参阅codex中的register\\u post\\u type条目)。无论您在自定义post类型注册中声明了哪些功能,您都需要为用户角色提供这些功能:

add_role(\'basic_contributor\', \'Basic Contributor\', array(
    \'read\' => true,
    \'edit_posts\' => false,
    \'edit_cpt_name\' => true, // True allows that capability
    \'edit_cpt_names\' => true,
    \'publish_cpt_names\' => true,
    \'edit_others_cpt_names\' => false, // Use false to explicitly deny
    \'delete_cpt_names\' => false
));

结束

相关推荐