Assign Custom Post Type Capabilities to Roles in WordPress

This code assigns capabilities for a custom post type to existing user roles in WordPress without a plugin. This function is only needed if you used something other than ‘post’ for the ‘capability_type‘ argument when registering your new custom post type. If you didn’t specify a ‘capability_type’ other than ‘post’, then you do NOT need this function.

EXPAND CAPABILITIES FOR A CUSTOM POST TYPE (‘isa_help’):

In this example function, the custom post type is ‘isa_help’. When you edit in your own custom post type, pay close attention to the plural ‘s’ at the end of some lines (lines 15-20, 23, 26, 29).

In this example, the Administrator is given capability to do it all. Editor, Author, and Contributor are given capability to read the public and private posts of this post type, but not edit, nor publish. This set up is useful so you can write helpful tips, make them private, and allow your clients to read them. This keeps your helpful articles out of the public view, but it also allows authors and contributors to read your ‘private’ helpful tips.


/******************** Expand Capabilities ********************/

add_action('init', 'isa_helpcapabilities');

function isa_helpcapabilities() {
		//There has to be a better way to do this?
		global $wp_roles;

		//Check if $wp_roles has been initialized
		if ( isset($wp_roles) ) {

$wp_roles->add_cap( 'administrator', 'edit_isa_help' );
$wp_roles->add_cap( 'administrator', 'read_isa_help' );
$wp_roles->add_cap( 'administrator', 'delete_isa_help' );
	$wp_roles->add_cap( 'administrator', 'publish_isa_helps' );
	$wp_roles->add_cap( 'administrator', 'edit_isa_helps' );
	$wp_roles->add_cap( 'administrator', 'edit_others_isa_helps' );
	$wp_roles->add_cap( 'administrator', 'delete_isa_helps' );
	$wp_roles->add_cap( 'administrator', 'delete_others_isa_helps' );
	$wp_roles->add_cap( 'administrator', 'read_private_isa_helps' );

		$wp_roles->add_cap( 'editor', 'read_isa_help' );
		$wp_roles->add_cap( 'editor', 'read_private_isa_helps' );

		$wp_roles->add_cap( 'author', 'read_isa_help' );
		$wp_roles->add_cap( 'author', 'read_private_isa_helps' );
		
		$wp_roles->add_cap( 'contributor', 'read_isa_help' );
		$wp_roles->add_cap( 'contributor', 'read_private_isa_helps' );

		}

}
/******************** end EXPAND CAPS ********************/

See more: ,

We've 2 Responses

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]