Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup simple User Roles for Wordpress Custom Post Type

Tags:

wordpress

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:

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

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.

Help. Thanks in advance.

like image 304
Ana Ban Avatar asked Apr 18 '12 09:04

Ana Ban


1 Answers

This really shouldn't be terribly difficult.

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',

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,

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.

like image 78
Chip Bennett Avatar answered Sep 30 '22 22:09

Chip Bennett