By default, anyone who can edit and edit others' posts can edit soliloquy. So what can you do to limit access to Soliloquy? It is simple as two steps:
Filter Soliloquy Post Type Registration Parameters
We want to over-ride soliloquy's default `capability_type` of `post` and give it a custom capability type.
add_filter( 'tgmsp_post_type_args', 'gs_tgmsp_post_type_args' ); | |
/** | |
* Filters Soliloquy post type registration parameters. | |
* | |
* @param array $args Soliloquy post type registration args. | |
* @return array $args Modified soliloquy post type registration args. | |
*/ | |
function gs_tgmsp_post_type_args( $args ) { | |
$args['capability_type'] = 'soliloquy'; | |
return $args; | |
} |
Doing this will give create the following caps:
[cap] => stdClass Object ( [edit_post] => edit_soliloquy [read_post] => read_soliloquy [delete_post] => delete_soliloquy [edit_posts] => edit_soliloquys [edit_others_posts] => edit_others_soliloquys [publish_posts] => publish_soliloquys [read_private_posts] => read_private_soliloquys [delete_posts] => delete_soliloquys [delete_private_posts] => delete_private_soliloquys [delete_published_posts] => delete_published_soliloquys [delete_others_posts] => delete_others_soliloquys [edit_private_posts] => edit_private_soliloquys [edit_published_posts] => edit_published_soliloquys )
If that was all you were to do, then no one would have access to Soliloquy. Now you need to add these capabilities to the designated role (e.g., administrator
).
Add Capabilities to Specific Role
This step is slightly more complicated than just modifying an argument. In short, we need to grab the post type object to get the newly created capabilities and add them to the role we want.
So, I want to hook immediately after soliloquy is registered. To do this, we hook into registered_post_type
, a little known hook introduced in WordPress 3.3.
add_action( 'registered_post_type', 'gs_tgmsp_add_caps_to_admin', 10, 2 ); | |
/** | |
* Add capabilities to soliloquy custom post type | |
* | |
* @param string $post_type Post type. | |
* @param array $args Original post type registration args. | |
*/ | |
function gs_tgmsp_add_caps_to_admin( $post_type, $args ) { |
Next, I want to make sure that I have the soliloquy
post type.
/** Make sure we have the correct post type */ | |
if ( 'soliloquy' !== $post_type ) return; |
Once I have the soliloquy
post type, I get the capabilities via get_post_type_object()
.
/** Get post type object to get capabilities */ | |
$pt = get_post_type_object( $post_type ); |
I then call my function (gs_add_caps_to_role()
) to add the custom capabilities created by WordPress to the designated role.
/** Add capabilities to administrator */ | |
gs_add_caps_to_role( 'administrator', $pt->cap ); | |
} |
Alternatively, you can use the $wp_post_types global variable, which could be very dangerous.
/** Get global post type object */ | |
global $wp_post_types; | |
/** Add capabilities to administrator */ | |
gs_add_caps_to_role( 'administrator', $wp_post_types[ $post_type ]->cap ); | |
} |
So here's the entire function:
<?php | |
add_filter( 'tgmsp_post_type_args', 'gs_tgmsp_post_type_args' ); | |
/** | |
* Filters Soliloquy post type registration parameters. | |
* | |
* @param array $args Soliloquy post type registration args. | |
* @return array $args Modified soliloquy post type registration args. | |
*/ | |
function gs_tgmsp_post_type_args( $args ) { | |
$args['capability_type'] = 'soliloquy'; | |
return $args; | |
} | |
add_action( 'registered_post_type', 'gs_tgmsp_add_caps_to_admin', 10, 2 ); | |
/** | |
* Add capabilities to soliloquy custom post type | |
* | |
* @param string $post_type Post type. | |
* @param array $args Original post type registration args. | |
*/ | |
function gs_tgmsp_add_caps_to_admin( $post_type, $args ) { | |
/** Make sure we have the correct post type */ | |
if ( 'soliloquy' !== $post_type ) return; | |
/** Get post type object to get capabilities */ | |
$pt = get_post_type_object( $post_type ); | |
/** Add capabilities to administrator */ | |
gs_add_caps_to_role( 'administrator', $pt->cap ); | |
} |
The function I called to add the capabilities to a specific role (gs_add_caps_to_role()
) is split out so I can use it in other places. If you want you can certainly place all this in one function. In this function, I simply ensure that I have an array and cycle through the capabilities adding them one-by-one to the designated role.
<?php | |
/** | |
* Add custom capabilities to role | |
* | |
* @param string $role Role to add capabilities. | |
* @param array $caps Custom capabilities. | |
*/ | |
function gs_add_caps_to_role( $role, $caps ) { | |
/** Convert object to an array */ | |
if ( is_object( $caps ) ) | |
$caps = json_decode( json_encode( $caps ), true ); | |
/** Make sure we have an array, bail otherwise */ | |
if ( !is_array( $caps ) ) return; | |
/** Get specified role object */ | |
$role = get_role( $role ); | |
/** Cycle through caps & add to role */ | |
foreach( array_values( $caps ) as $cap ) | |
$role->add_cap( $cap ); | |
} |
So, if I wanted to add these capabilities to the administrator
and editor
, I would do this:
/** Add capabilities to roles */ | |
foreach( array( 'administrator', 'editor', ) as $role ) | |
gs_add_caps_to_role( $role, $pt->cap ); |
Leave a Reply