首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何仅为特定用户提供自定义帖子类型的访问权限

如何仅为特定用户提供自定义帖子类型的访问权限
EN

Stack Overflow用户
提问于 2019-09-16 15:26:44
回答 1查看 2.2K关注 0票数 3

在我的代码中,我有一个自定义的帖子类型job.我创建了一个名为job manager.Now的角色,我想为职务manager.Only授予添加、编辑、删除职务的权限他可以访问职务帖子type.hoe w来实现这一点...

我尝试通过以下代码创建作业经理角色

代码语言:javascript
复制
          function add_job_manager_role(){
           add_role(
                'job_manager',
                 'Job Manager',
                   array(
                    'read'          => true,
                    'edit_posts'    => false,
                    'delete_posts'  => false,
                    'publish_posts' => false,
                    'upload_files'  => true
           )
        );
     }
   add_action( 'admin_init', 'add_job_manager_role', 4 ); 

如何授予作业经理添加、删除、编辑自定义帖子类型作业的权限

任何帮助都非常感谢提前.Thanks ...

EN

Stack Overflow用户

发布于 2019-09-16 17:09:29

当您添加角色时,您还必须添加功能,如下所示:

代码语言:javascript
复制
/**
add CPT capabilites to Role
*/
add_action('admin_init','o99_add_role_caps',999);

function o99_add_role_caps() {

    $role = get_role('my_role');      // ex. job_manager         
    $role->add_cap( 'read_my_CPT');
    $role->add_cap( 'edit_my_CPT' );
    $role->add_cap( 'edit_my_CPT' );
    $role->add_cap( 'edit_other_my_CPT' );
    $role->add_cap( 'edit_published_my_CPT' );
    $role->add_cap( 'publish_my_CPT' );
    $role->add_cap( 'read_private_my_CPT' );
    $role->add_cap( 'delete_my_CPT' );


}

当然,当您在创建或修改功能参数时,my_CPT是您的自定义帖子类型,您所做的事情如下:

代码语言:javascript
复制
function change_capabilities_of_CPT( $args, $post_type ){

 // Do not filter any other post type
 if ( 'my_CPT' !== $post_type ) { // my_CPT == Custom Post Type == 'job' or other

     // if other post_types return original arguments
     return $args;

 }


// This is the important part of the capabilities 
/// which you can also do on creation ( and not by filtering like in this example )


 // Change the capabilities of my_CPT post_type
 $args['capabilities'] = array(
            'edit_post' => 'edit_my_CPT',
            'edit_posts' => 'edit_my_CPT',
            'edit_others_posts' => 'edit_other_my_CPT',
            'publish_posts' => 'publish_my_CPT',
            'read_post' => 'read_my_CPT ',
            'read_private_posts' => 'read_private_my_CPT',
            'delete_post' => 'delete_my_CPT'
        );

  // Return the new arguments
  return $args;

}

编辑我

有关详细信息,请访问:

为了能够控制CPT,每个操作都涉及其他几个capabilities

例如,为了publish_my_CPT和为了编辑,你需要edit_my_CPT && edit_other_my_CPT && read_my_CPT && read_private_my_CPT等等。请查看capabilities in the Codex,通过发布的代码,您可以将_my_CPT (例如- _job或其他任何CPT )添加到这些功能中,从而使您能够获得所需的结果。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57952159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档