Ana Ban
Ana Ban

Reputation: 1405

How to setup simple User Roles for Wordpress Custom Post Type

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:

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.

Upvotes: 7

Views: 17921

Answers (1)

Chip Bennett
Chip Bennett

Reputation: 393

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.

Upvotes: 4

Related Questions