Allowing custom role access to custom post type in back end

It’s hard to troubleshoot the above code because it’s only a part of the actual code, but here’s the minimum plugin needed to register a custom post type (called Example) and a custom role (Blog Manager) that has access to the Example custom post type.

This can be used as part of a theme’s functions.php file as well. Just use the theme activation and deactivation hooks instead.

<?php
/**
 * Plugin Name: WPSE 186337
 * Description: Debug WordPress StackExchange question 186337
 * Plugin URI: https://wordpress.stackexchange.com/questions/186337/
 * Author: Nathan Johnson
 * Licence: GPL2+
 * Licence URI: https://www.gnu.org/licenses/gpl-2.0.en.html
 */

//* Don't access this file directly
defined( 'ABSPATH' ) or die();

//* Add action to init to register custom post type
add_action( 'init', 'se186337_init' );

//* Register activation hook to add Blog Manager role
register_activation_hook( __FILE__ , 'se186337_activation' );

//* Register deactivation hook to remove Blog Manager role
register_deactivation_hook( __FILE__ , 'se186337_deactivation' );

function se186337_activation() {
  $caps = [
    //* Meta capabilities
    'read'                   => true,
    'edit_blog'              => true,
    'read_blog'              => true,
    'delete_blog'            => true,

    //* Primitive capabilities used outside of map_meta_cap()
    'edit_blogs'             => true,
    'edit_others_blogs'      => true,
    'publish_blogs'          => true,
    'read_private_blogs'     => true,

    //* Primitive capabilities used within of map_meta_cap()
    'delete_blogs'           => true,
    'delete_private_blogs'   => true,
    'delete_published_blogs' => true,
    'delete_others_blogs'    => true,
    'edit_private_blogs'     => true,
    'edit_published_blogs'   => true,
  ];

  add_role( 'blog_manager', 'Blog Manager', $caps );
}

function se186337_deactivation() {
  remove_role( 'blog_manager' );
}

function se186337_init() {
  $labels = [
    'name'          => __( 'Examples' ),
    'singular_name' => __( 'Example' ),
  ];
  $args = [
    'labels'          => $labels,
    'public'          => true,
    'has_archive'     => true,
    'capability_type' => 'blog',
    'map_meta_cap'    => true,
  ];
  register_post_type( 'examples', $args );
}

Leave a Comment