简体   繁体   English

如何仅授予特定用户自定义帖子类型的访问权限

[英]how to give only access to custom post type for a particular user

In my code i have a custom post type job.I created a role called job manager.Now i want to give permissions to add,edit ,delete a job for job manager.Only he can access job post type.hoe w to achieve this.... 在我的代码中,我有一个自定义职位类型的job。我创建了一个名为job manager的角色。现在,我想授予为job manager添加,编辑,删除职位的权限。只有他才能访问job post type.hoe w以实现此目的。 ....

I tried to create job manager role by following code 我尝试通过以下代码创建作业经理角色

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

how to give permissions to job manager to add ,delete,edit custom post type job 如何授予工作经理权限以添加,删除,编辑自定义帖子类型的工作

any help greatly appreciated .Thanks in advance .... 任何帮助表示赞赏。在此先感谢....

When you add the role you have to add the capabilities also like so : 添加角色时,还必须添加功能,如下所示:

/**
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 is of course your Custom Post Type when in creating you gave the capabilities arguments or modifying it you did something like : 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;

}

Edit I 编辑我

For further info : 有关更多信息:

In order to be able to control the CPT, there are several other capabilities involved for each operation. 为了能够控制CPT,每个操作还涉及其他几种capabilities

Just for example in order to publish_my_CPT and in order to edit you will need the edit_my_CPT && edit_other_my_CPT && read_my_CPT && read_private_my_CPT and so on. 仅举例来说,要发布publish_my_CPT并进行编辑,您将需要edit_my_CPT && edit_other_my_CPT && read_my_CPT && read_private_my_CPT等。 please look at capabilities in the Codex and with the code posted you can add the _my_CPT (eg - _job or whatever CPT ) to those capabilities thus allowing you to achieve he desired result. 请查看法典中的功能,并在发布代码_my_CPT (例如_job或其他CPT)添加到这些功能中,从而使您能够获得所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅允许一个特定用户访问更新页面? - How to give access to an update page to one particular user only? 如何在drupal 7中仅授予特定用户“添加内容”访问权限? - How to give only “Add content” access permission for a particular users in drupal 7? 如何访问wordpress自定义帖子类型对象 - How to access wordpress custom post type Object 如何仅在 wordpress 中为用户显示自定义帖子 - How to display custom post for user only in wordpress 如何限制新用户角色只能访问常规帖子,而不是自定义帖子类型? - How to limit a new user role to access only regular posts, not custom post types? WordPress,自定义帖子类型如果超过1个帖子类型,则给出404 - Wordpress, Custom post Type give 404 if more than 1 post type 如何在WordPress中为自定义用户角色启用自定义帖子类型 - How to enable a custom post type to custom user role in WordPress WordPress:自定义用户角色无法访问自定义帖子类型 | “抱歉,您无权访问此页面” - WordPress: Custom User Role cannot access Custom Post Type | “Sorry, you are not allowed to access this page” 如何限制特定用户访问特定网页 - How to restrict a particular user to access the particular webpage 自定义帖子类型仅返回第一个帖子 - Custom Post Type is only returning the first post
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM