【问题标题】:how to give only access to custom post type for a particular user如何仅授予特定用户对自定义帖子类型的访问权限
【发布时间】:2020-01-17 00:20:19
【问题描述】:

在我的代码中,我有一个自定义职位类型的职位。我创建了一个名为职位经理的角色。现在我想授予职位经理添加、编辑、删除职位的权限。只有他可以访问职位职位类型。锄头实现这一目标....

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

          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 ); 

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

非常感谢任何帮助。在此先感谢....

【问题讨论】:

  • 您已经添加了一个角色 - 所以给他编辑该帖子类型的能力,并将这些能力也放入帖子类型的注册中..
  • 基本上:遵循本指南:3.7designs.co/blog/2014/08/…
  • 另外,如果您想轻松管理角色,您可以使用会员插件,这正是您想要的。 fr.wordpress.org/plugins/members
  • 我已经有帖子类型的工作。我只想授予该帖子类型的权限。仍然需要注册帖子类型?
  • 我没有从使用上面的代码中得到。我希望工作经理只能添加、编辑、删除工作职位类型如何做到这一点

标签: php wordpress arguments roles wordpress-capabilities


【解决方案1】:

当您添加角色时,您还必须像这样添加功能:

/**
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 当然是您的自定义帖子类型,在创建时您提供了功能参数或修改它时您做了类似的事情:

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 )添加到这些功能中,从而使您能够实现他想要的结果。

【讨论】:

  • 我没有从使用上面的代码中得到。我希望工作经理只能添加、编辑、删除工作职位类型如何做到这一点
  • 在发布的代码中您拥有所有权限,您可以随时过滤您想要的权限。您需要了解 add 例如是 publishread 当然您也需要。
  • 我使用相同的代码模式来完成这项工作,但无法这样做,wordpress.stackexchange.com/questions/402045/… 有人可以指导吗?
猜你喜欢
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
  • 2017-11-24
  • 2020-02-17
  • 1970-01-01
相关资源
最近更新 更多