10

So I'm having some issues with this and I can't see why. I just need a custom role that can access the blog in the back end.

I've added a new post type with a Capability type of blog and a new user role with all the caps fr it that would allow admin access users to add/edit the custom post type. This works for admins and they can access the post type in the back end. However users of my custom role can not get into the back end at all.

Post type args of note

"capability_type" => 'blog',
"map_meta_cap" => true,

Register role

function add_blog_manager_role(){
    add_role(
        'blog_manager',
        'Blog Manager',
        array(
            'read' => true,
            'edit_posts' => false,
            'delete_posts' => false,
            'publish_posts' => false,
            'upload_files' => true
        )
    );
}
add_action( 'admin_init', 'add_blog_manager_role', 4 );

Add Caps

function add_blog_role_caps() {
    $roles = array('blog_manager', 'editor','administrator');
    foreach($roles as $the_role) {
        $role = get_role($the_role);
        $role->add_cap( 'read' );
        $role->add_cap( 'read_blog');
        $role->add_cap( 'read_private_blog' );
        $role->add_cap( 'edit_blog' );
        $role->add_cap( 'edit_others_blog' );
        $role->add_cap( 'edit_published_blog' );
        $role->add_cap( 'publish_blog' );
        $role->add_cap( 'delete_others_blog' );
        $role->add_cap( 'delete_private_blog' );
        $role->add_cap( 'delete_published_blog' );
    }
}
add_action('admin_init', 'add_blog_role_caps', 5 );

I've been googeling frantically trying to find the cause of this. I've tried with plural, non plural caps, tried adding capabilities into the post type args. However I'm never able to get into the back end. I've not got any other code in the theme that might kick users out of the admin (I removed my own code that kicked them out while testing this)

Edit Here you can see a dump of the blog_manager capabilities from the database, there is a fair bit of testing BS left in there, bu/t that shouldn't stop them being able to login from what I know.

'blog_manager' => array (
    'name' => 'Blog Manager',
    'capabilities' => array (
        'read' => true,
        'edit_posts' => false,
        'delete_posts' => false,
        'publish_posts' => false,
        'upload_files' => true,
        'read_blog' => true,
        'read_private_blog' => true,
        'edit_blog' => true,
        'edit_others_blog' => true,
        'edit_published_blog' => true,
        'publish_blog' => true,
        'delete_others_blog' => true,
        'delete_private_blog' => true,
        'delete_published_blog' => true,
        'blog' => true,
        'read_private_blogs' => true,
        'edit_blogs' => true,
        'edit_others_blogs' => true,
        'edit_published_blogs' => true,
        'publish_blogs' => true,
        'delete_others_blogs' => true,
        'delete_private_blogs' => true,
        'delete_published_blogs' => true,
        'delete_blogs' => true,
        'delete_blog' => true,
    ),
)
4
  • 2
    Note that roles and capabilities are persistently saved. If you had some earlier version of this that didn't allow access it might still be there as part of role. Dump the persistent data and see if there is anything set that shouldn't be.
    – Rarst
    Commented May 5, 2015 at 14:32
  • I added a dump of the database capabilities. I only have the 3 post capabilities set to false that I have in the code above. Commented May 5, 2015 at 14:54
  • I would install the User Role Editor plugin - wordpress.org/plugins/user-role-editor. Then manually inspect the differences between the users/roles - there may be something conflicting or missing.
    – Welcher
    Commented May 20, 2015 at 23:50
  • Hi @ChrisMorris, how did you go with this? Did you end up finding a solution?
    – Tim Malone
    Commented Jun 29, 2016 at 8:18

2 Answers 2

8

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 );
}
1
  • in the function se186337_activation() you have assigned the capabilities, however why have you not used the $add->cap function to be used inside another function with a foreach loop to add capabilities that way? hope that makes sense...
    – Krys
    Commented Mar 22, 2022 at 6:08
0

If you are ok with plugin than you can use velow plugin.

https://wordpress.org/plugins/capability-manager-enhanced/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.