如何为 Wordpress 自定义帖子类型设置简单的用户角色 [英] How to setup simple User Roles for Wordpress Custom Post Type

查看:20
本文介绍了如何为 Wordpress 自定义帖子类型设置简单的用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Wordpress 为临时工/兼职人员开发一个系统.为此,我正在制作一个新的自定义帖子类型 EMPLOYEE,我需要 2 个相应的用户 AGENT 和 CUSTOMER:

I'm working on a system for temps/part-timers using Wordpress. To go about this I'm making a new custom post type EMPLOYEE and I need 2 corresponding users AGENT and CUSTOMER for it:

  • AGENT 用户可以创建编辑自己的 EMPLOYEE,但不能编辑其他 AGENT 的 EMPLOYEE.
  • 所有代理、客户和公众都可以查看评论员工帖子,
  • 这也意味着 CUSTOMER 用户只能查看所有 EMPLOYEE 帖子.
  • 需要将 CUSTOMER 用户与普通大众区分开来,因为一旦他们雇用了 EMPLOYEE,系统需要将他们与 EMPLOYEE 相关联,因此 CUSTOMER 需要是登录用户.
  • 最后,AGENT 和 CUSTOMER 用户都可以创建/编辑他们自己的普通 Wordpress 用户配置文件,并使用他们的用户名/密码组合登录.
  • The AGENT user can create and edit his own EMPLOYEE(s), but not edit the EMPLOYEE(s) of other AGENT(s).
  • The EMPLOYEE post can be viewed and commented on by all AGENTs, CUSTOMERs and the general public,
  • which also implies that the CUSTOMER user can only view all EMPLOYEE posts.
  • The CUSTOMER user needs to be differentiated from the general public because once they hire an EMPLOYEE, the system needs to associate them with the EMPLOYEE, thus the need for the CUSTOMER to be a signed-in user.
  • Finally, both AGENT and CUSTOMER users can create/edit their own vanilla Wordpress user profiles and sign-in with their username/password combination.

我如何执行此安排?有关用户和功能的在线文档让我摸不着头脑,手忙脚乱.到目前为止,这是我的自定义帖子类型注册,我目前正在为此帖子类型设置其他信息的元框:

How do I execute this arrangement? The online documentation on users and capabilities had me pulling my hair and runnin around in circles. So far here's my custom post type registration, and I'm currently setting up the meta boxes for other info for this post type:

register_post_type( 'employee',
    array(
        'labels' => array(
            'name' => __('Employees','tdom'),
            'singular_name' => __('Employee','tdom'),
            'add_new' => __( 'Add New' ),
            'add_new_item' => __( 'Add New Employee' ),
            'edit' => __( 'Edit' ),
            'edit_item' => __( 'Edit Employee' ),
            'new_item' => __( 'New Employee' ),
            'view' => __( 'View Employee' ),
            'view_item' => __( 'View Employee' ),
            'search_items' => __( 'Search Employees' ),
            'not_found' => __( 'No Employees found' ),
            'not_found_in_trash' => __( 'No Employees found in Trash' ),
            'parent' => __( 'Parent Employee' )
        ),
        'public' => true,
        'show_ui' => true,
        'query_var' => true,
        'menu_icon' => get_stylesheet_directory_uri() . '/images/emp_icon.png',
        'menu_position' => 4,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'supports' => array('title', 'thumbnail', 'author')
    )
);

我遇到过 Justin Tadlock 的 Members 插件(在 Google 和论坛上非常流行),但我希望我的要求足够简单,不再使用插件.这也有点过分了.

I've come across Justin Tadlock's Members plugin (very popular on Google and forums), but I was hoping my requirements are simple enough not to use a plugin anymore. It's also a bit too much to wrap my head around.

帮助.提前致谢.

推荐答案

这真的不应该太困难.

第一步是创建一个自定义功能类型以对应于您的自定义帖子类型,通过传递给 register_post_type().您正在使用默认值,即:

The first step is to create a custom capability type to correspond to your custom post type, via the 'capability_type' parameter passed to register_post_type(). You're using the default, i.e.:

'capability_type' => 'post',

将其设置为其他内容,例如 'employee',并且根据 Codex,还将 'map_meta_cap' 设置为 true:

Set that to something else, perhaps 'employee', and as per the Codex, also set 'map_meta_cap' to true:

'capability_type' => 'employee',
'map_meta_cap'    => true,

下一步是通过传递给您对 add_role().对于employee"用户角色,您需要添加 edit_employee 等(edit_、delete_ 等),对于agent"用户角色,您需要添加 edit_employee 等人,以及 edit_others_employee 等人

The next step is to add the related capabilities to your custom users, via the $capabilities parameter passed to your call to add_role(). For the "employee" user role, you'll want to add edit_employee et al (edit_, delete_, etc.), and for the "agent" user role, you'll want to include edit_employee et al, along with edit_others_employee et al.

这篇关于如何为 Wordpress 自定义帖子类型设置简单的用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆