WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Aug 23 2013

Limit Users to Soliloquy: How to Add a Custom Capability to Soliloquy?

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:

  1. Filter Soliloquy Post Type Registration Parameters
  2. Add Capabilities to Specific Role

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;
}
view raw soliloquy-capabilities.php hosted with ❤ by GitHub

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 ) {
view raw soliloquy-capabilities.php hosted with ❤ by GitHub

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;
view raw soliloquy-capabilities.php hosted with ❤ by GitHub

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 );
view raw soliloquy-capabilities.php hosted with ❤ by GitHub

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 );
}
view raw soliloquy-capabilities.php hosted with ❤ by GitHub

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 );
}
view raw soliloquy-capabilitiesalt.php hosted with ❤ by GitHub

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 );
}
view raw soliloquy-capabilities.php hosted with ❤ by GitHub

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 );
}
view raw add-capabilities-to-role.php hosted with ❤ by GitHub

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 );
view raw soliloquy-capabilitiesmultiple.php hosted with ❤ by GitHub

Written by Travis Smith · Categorized: Plugins, WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in