Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Last active December 21, 2015 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpsmith/6321700 to your computer and use it in GitHub Desktop.
Save wpsmith/6321700 to your computer and use it in GitHub Desktop.
PHP: Add custom capability_type to soliloquy.
<?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 );
}
<?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 );
}
<?php
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 global post type object */
global $wp_post_types;
/** Add capabilities to administrator */
gs_add_caps_to_role( 'administrator', $wp_post_types[ $post_type ]->cap );
}
<?php
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 roles */
foreach( array( 'administrator', 'editor', ) as $role )
gs_add_caps_to_role( $role, $pt->cap );
}
@wpsmith
Copy link
Author

wpsmith commented Aug 23, 2013

A full tutorial explaining how to limit users to Soliloquy and how to add a custom capability to Soliloquy can be found at http://wpsmith.net/2013/plugins/limit-users-to-soliloquy-how-to-add-a-custom-capability-to-soliloquy/ (Shortlink: http://wpsmith.net/?p=22740).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment