9

I cannot get it so that my user role for a supplier can read the shipments post type. It shows up in their menu but when you click on it you get you are not allowed to view this page error message.

It works if I add_cap('read_posts') but I don't want them to view the regular posts just the shipments post type.

I have a suppliers user role with the following capabilities.

WP_Role Object
(
    [name] => supplier
    [capabilities] => Array
        (
            [read] => 1
            [edit_shipment] => 1
            [read_shipment] => 1
            [edit_others_shipments] => 1
            [publish_shipments] => 1
            [read_private_shipments] => 1
            [edit_shipments] => 1
            [create_shipment] => 1
            [read_shipments] => 1
        )

)

And I have setup the post type for shipments with the following

function shipment_post_type() {
  $labels = array(
    'name'                  => _x( 'Shipments', 'Post Type General Name', 'sage' ),
    'singular_name'         => _x( 'Shipment', 'Post Type Singular Name', 'sage' ),
    'menu_name'             => __( 'Shipments', 'sage' ),
    'name_admin_bar'        => __( 'Shipments', 'sage' ),
    'archives'              => __( 'Shipment Archives', 'sage' ),
    'parent_item_colon'     => __( 'Parent shipment:', 'sage' ),
    'all_items'             => __( 'All shipments', 'sage' ),
    'add_new_item'          => __( 'Add New shipment', 'sage' ),
    'new_item'              => __( 'New shipment', 'sage' ),
    'edit_item'             => __( 'Edit shipment', 'sage' ),
    'update_item'           => __( 'Update shipment', 'sage' ),
    'view_item'             => __( 'View shipment', 'sage' ),
    'search_items'          => __( 'Search shipments', 'sage' ),
    'not_found'             => __( 'Not found', 'sage' ),
    'not_found_in_trash'    => __( 'Not found in Trash', 'sage' ),
    'featured_image'        => __( 'Featured Image', 'sage' ),
    'set_featured_image'    => __( 'Set shipment image', 'sage' ),
    'remove_featured_image' => __( 'Remove shipment image', 'sage' ),
    'use_featured_image'    => __( 'Use as shipment image', 'sage' ),
    'insert_into_item'      => __( 'Insert into shipment', 'sage' ),
    'uploaded_to_this_item' => __( 'Uploaded to this shipment', 'sage' ),
    'items_list'            => __( 'shipments list', 'sage' ),
    'items_list_navigation' => __( 'Constests list navigation', 'sage' ),
    'filter_items_list'     => __( 'Filter shipments list', 'sage' ),
  );
  $args = array(
    'label'                 => __( 'shipments', 'sage' ),
    'description'           => __( 'Manage all shipments, sweepstakes and giveaways.', 'sage' ),
    'labels'                => $labels,
    'supports'              => array( 'revisions' ),
    'taxonomies'            => array( '' ),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-archive',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => false,
    'can_export'            => true,
    'has_archive'           => false,
    'exclude_from_search'   => true,
    'publicly_queryable'    => true,
    'map_meta_cap' => true,
    'capabilities' => array(
      'edit_post'          => 'edit_shipment',
      'read_post'          => 'read_shipment',
      'read_posts'         => 'read_shipments',
      'delete_post'        => 'delete_shipment',
      'delete_posts'       => 'delete_shipments',
      'edit_posts'         => 'edit_shipments',
      'edit_others_posts'  => 'edit_others_shipments',
      'publish_posts'      => 'publish_shipments',
      'read_private_posts' => 'read_private_shipments',
      'create_posts'       => 'create_shipments',
    ),
  );
  register_post_type( 'shipment', $args );

}
add_action( 'init', 'shipment_post_type', 0 );
  • Check this solution: wordpress.stackexchange.com/a/108375/38771 – P-S Apr 5 '18 at 8:09
  • I've read this multiple times... followed instructions everything I could do. Still get the you are not allowed to access until I give the create_shipments permission. – Nicholas Koskowski Apr 5 '18 at 17:47
  • 3
    Can you share your code where you are adding the role with add_role() and the caps add_cap()? – Mat Apr 10 '18 at 21:06
  • 1
    Are you trying to let the supplier read this post type on the front end or on the back end? – Myles Apr 11 '18 at 14:09
  • And is this code in a plugin or a theme? Plugin is better, but either way, with more information about your setup I'm sure I can provide an even more comprehensive solution for you – Myles Apr 11 '18 at 20:21
4
+50

Your custom post type looks like it's set up properly. It works on my test install. Try this instead of whatever add_role and add_cap code you're currently using. (For testing purposes only. Don't use it in production code, for reasons outlined below.) It's working for me:

function prefix_set_up_supplier_role(){
remove_role( 'supplier' );
add_role( 'supplier', 'Supplier', array(
        'read'                      => true,
        'edit_shipment'             => true,
        'read_shipment'             => true,
        'read_shipments'            => true,
        'delete_shipment'           => true,
        'delete_shipments'          => true,
        'edit_shipments'            => true,
        'edit_others_shipments'     => true,
        'publish_shipments'         => true,
        'read_private_shipments'    => true,
        'create_shipments'          => true,
    )
);
}
add_action( 'init', 'prefix_set_up_supplier_role' );

It's very important to remember that adding user roles and capabilities actually saves data to the database. So if you had a version of your code before that didn't quite work, then added something that would make it work, it might not have taken effect if there is still old data in your database. add_role() returns null if the role already exists in the database. For production code, you should actually be using the plugin activation and deactivation hooks for this stuff instead of running it every time, like this:

register_activation_hook( __FILE__, 'prefix_activate' );
function prefix_activate(){
    add_role( 'supplier', 'Supplier', array(
        'read'                      => true,
        'edit_shipment'             => true,
        'read_shipment'             => true,
        'read_shipments'            => true,
        'delete_shipment'           => true,
        'delete_shipments'          => true,
        'edit_shipments'            => true,
        'edit_others_shipments'     => true,
        'publish_shipments'         => true,
        'read_private_shipments'    => true,
        'create_shipments'          => true,
    )
);
}

register_deactivation_hook( __FILE__, 'prefix_deactivate' );
function prefix_deactivate(){
    remove_role( 'supplier' );
}
  • 2
    add_role() and remove_role() shouldn't be executed in every request. Note that once a role is added to database, you don't to add it again. Usually you add roles on register_activation_hook and remove roles onregister_deactivation_hook/register_unistall_hook; other than that, you should execute remove_role()/add_role() only if it is really needed. – cybmeta Apr 13 '18 at 6:29
  • I know, and I've mentioned that in my answer as well. But it can still be a quick way to diagnose problems in development so that you don't have to click deactivate and then activate every time you make a change to the code. Then, when it's working, move it to activation and deactivation hooks – Myles Apr 13 '18 at 13:04
  • 1
    @cybmeta That is true. I edited my answer to reflect how best practices can be followed. – Myles Apr 13 '18 at 15:57
0

In your role object, you have the capability 'create_shipment' where it should actually say 'create_shipments'. Seems like there might be an 's' missing in your code wherever you're adding that capability.

0

You might try:

add_action( 'init', 'add_my_caps');
function add_my_caps() {
    global $wp_roles;

    if ( isset($wp_roles) ) {
        $wp_roles->add_cap( 'editor', 'edit_shipment' );
        $wp_roles->add_cap( 'editor', 'read_shipment' );
        $wp_roles->add_cap( 'editor', 'delete_shipment' );
        $wp_roles->add_cap( 'editor', 'publish_shipments' );
        $wp_roles->add_cap( 'editor', 'edit_shipments' );
        $wp_roles->add_cap( 'editor', 'edit_others_shipments' );
        $wp_roles->add_cap( 'editor', 'delete_shipments' );
        $wp_roles->add_cap( 'editor', 'delete_others_shipments' );
        $wp_roles->add_cap( 'editor', 'read_private_shipments' );
        ....
0

Try this out. 'dms_document' is the custom post type.

function jgd_add_role_caps() {
    // Add the roles you'd like to administer the custom post types
    $roles = array(
        'deity_user',
        'admin_user'
    );

    // Loop through each role and assign capabilities
    foreach($roles as $the_role) { 
        $role = get_role($the_role);

        $role->add_cap('read');
        $role->add_cap('read_dms_document');
        $role->add_cap('read_private_dms_documents');
        $role->add_cap('edit_dms_document');
        $role->add_cap('edit_dms_documents');
        $role->add_cap('edit_others_dms_documents');
        $role->add_cap('edit_published_dms_documents');
        $role->add_cap('publish_dms_documents');
        $role->add_cap('delete_others_dms_documents');
        $role->add_cap('delete_private_dms_documents');
        $role->add_cap('delete_dms_documents');
        $role->add_cap('delete_post_dms_documents');
        $role->add_cap('delete_published_dms_documents');
        $role->add_cap('delete_draft_dms_documents');
        $role->add_cap('delete_others_posts_dms_documents');
        $role->add_cap('delete_others_posts_dms_document');
        $role->add_cap('delete_posts_dms_documents');
        $role->add_cap('delete_posts_dms_document');
    }
}
add_action('admin_init','jgd_add_role_caps', 999);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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