Hooks

Docs Hooks Free · PHP hooks

modal_kit_post_type_capabilities

filter

Free

PHP

Change the capabilities needed to create, edit, publish and delete modals. By default Authors and Contributors draft, and Editors publish.

Filters the capabilities passed to register_post_type() for the mk_modal post type, on init. Each key is one of the post type’s primitive capabilities and each value is the capability a user needs for it. map_meta_cap is on, so checks such as edit_post for one modal resolve through this map.

By default each one maps to its post equivalent, except going live: publishing a modal, and editing or deleting a published one, need edit_others_posts, because a live modal can show on every page of the site.

Parameters

Parameter

Description

$caps

array<string, string>

Post type capability => required capability. Keys: edit_posts, edit_others_posts, edit_private_posts, edit_published_posts, publish_posts, read_private_posts, delete_posts, delete_others_posts, delete_private_posts, delete_published_posts, create_posts and read.

Returns

array<string, string> The map, cast to an array. A key you remove falls back to core’s name for the mk_modal capability type, such as edit_mk_modals, which no role has.

Examples

Only Administrators put modals live

Editors can still draft modals and edit other people’s drafts.

PHP

add_filter( 'modal_kit_post_type_capabilities', function ( $caps ) {
  return array_merge(
    $caps,
    array(
      'publish_posts'          => 'manage_options',
      'edit_published_posts'   => 'manage_options',
      'delete_published_posts' => 'manage_options',
    )
  );
} );

A capability of its own for going live

Put this in your plugin’s main file. Activating the plugin gives the capability to Administrators and Editors; a role editor can give it to anyone else.

PHP

add_filter( 'modal_kit_post_type_capabilities', function ( $caps ) {
  $live = array(
    'publish_posts',
    'edit_published_posts',
    'delete_published_posts',
  );

  foreach ( $live as $cap ) {
    $caps[ $cap ] = 'my_publish_modals';
  }

  return $caps;
} );

register_activation_hook( __FILE__, function () {
  foreach ( array( 'administrator', 'editor' ) as $name ) {
    $role = get_role( $name );

    if ( $role ) {
      $role->add_cap( 'my_publish_modals' );
    }
  }
} );

Good to know

The filter runs once, when the post type is registered on init (priority 10). Add it when your plugin loads, not from a later hook.

The admin menu, the REST API and the editor follow the mapped capabilities. Opening a page with ?modal=preview, which shows a modal whatever its rules, needs the mapped edit_others_posts.

A few checks use core’s edit_posts directly, whatever you map: the Duplicate link in the modal list, and whether modals appear in the editor’s link search (/wp/v2/search).

The homepage behind the modal in the editor has its own capability: see modal_kit_backdrop_capability.

Applied in modal-kit/includes/PostType.php.

Need a hook that isn’t here?