0

I am currently working on making a custom user role "adoption_agency". Along with this custom role, i have created a custom post type called "adoptions". I have given "administrators" and "shop_manager" full access to this post type, which should allow them to delete, update or edit the post. When I login as an "adoption_agency" , "shop_manager" or "administrator" user I am able to view the custom post type on my dashbaord and i am able to access it. The issue i am having is that when I go to add new adoption and publish it, it says "You are not allowed to edit this post." I would like for the "adoption_agency" users to only have the "adoptions" post type available on their dashboards. I have added my code below, I have been trying for a few days now and i cant seem to get it working. I would really appreciate the help.

Here is the my custom post type code:

// Register Custom Post Type
function custom_post_type_adoption() {

    $labels = array(
        'name'                  => _x( 'adoptions', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'adoption', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Adoptions', 'text_domain' ),
        'name_admin_bar'        => __( 'Adoptions', 'text_domain' ),
        'archives'              => __( 'Adoption Archives', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
        'all_items'             => __( 'All Adoptions', 'text_domain' ),
        'add_new_item'          => __( 'Add New Adoption', 'text_domain' ),
        'add_new'               => __( 'Add New Adoption', 'text_domain' ),
        'new_item'              => __( 'New Adoption', 'text_domain' ),
        'edit_item'             => __( 'Edit Adoption', 'text_domain' ),
        'update_item'           => __( 'Update Adoption', 'text_domain' ),
        'view_item'             => __( 'View Adoption', 'text_domain' ),
        'search_items'          => __( 'Search Adoption', 'text_domain' ),
        'not_found'             => __( 'Adoption Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Adoption Not found in Trash', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into Adoption', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this Adoption', 'text_domain' ),
        'items_list'            => __( 'Adoption list', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'adoption', 'text_domain' ),
        'description'           => __( 'Post type for Adoptions', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'excerpt', 'author', 'thumbnail', ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-smiley',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,        
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type' => array("adoption", "adoptions"),
    );
    register_post_type( 'adoptions', $args );

}
add_action( 'init', 'custom_post_type_adoption', 0 );

Here is my custom user role code:

function add_adoption_caps_to_admin() {
  $caps = array(
    'read',
    'read_adoptions',
    'read_private_adoptions',
    'edit_adoptions',
    'edit_private_adoptions',
    'edit_published_adoptions',
    'edit_others_adoptions',
    'publish_adoptions',
    'delete_adoptions',
    'delete_private_adoptions',
    'delete_published_adoptions',
    'delete_others_adoptions',
    'upload_files',
    'edit_files',

  );
  $roles = array(
    get_role( 'administrator' ),
    get_role( 'editor' ),
    get_role( 'shop_manager' ),
  );
  foreach ($roles as $role) {
    foreach ($caps as $cap) {
      $role->add_cap( $cap );
    }
  }
}
add_action( 'after_setup_theme', 'add_adoption_caps_to_admin' );


function adoption_agency_setup() {
  add_role( 'adoption_agency', 'Adoption Agency');
}
add_action( 'after_setup_theme', 'adoption_agency_setup' );




function add_adoption_caps_to_non_admin_roles() {
  # Everyone gets these capabilities:
  $caps = array(
    'read',
    'read_adoptions',
    'read_private_adoptions',
    'edit_adoptions',
    'edit_published_adoptions',
    'publish_adoptions',
    'delete_adoptions',
    'delete_published_adoptions',
    'upload_files',
    'edit_files',
  );
  $roles = array(

    get_role( 'adoption_agency' ),

  );
  foreach ($roles as $role) {
    foreach ($caps as $cap) {
      $role->add_cap( $cap );
    }
  }
}
add_action( 'after_setup_theme', 'add_adoption_caps_to_non_admin_roles' );




function custom_remove_menus(){

    // Get current user's data
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;

    // Check user's roles
    $user = new WP_User( $user_id );
    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
        if( in_array( 'adoption_agency', $user->roles ) ) {
            // Remove menu items
            remove_menu_page( 'upload.php');
            remove_menu_page( 'index.php');

        }
    }
}

add_action( 'admin_menu', 'custom_remove_menus' );
2

1 Answer 1

2

I thought I would try to clarify my comments:

You only get a few things out of setting your capability_types the way you have above, which it is explained in Note 2 of capabilities.

Here is what I see looking at $GLOBALS['wp_post_types']['adoptions']->cap:

stdClass Object(
    [edit_post] => edit_adoption
    [read_post] => read_adoption
    [delete_post] => delete_adoption
    [edit_posts] => edit_adoptions
    [edit_others_posts] => edit_others_adoptions
    [publish_posts] => publish_adoptions
    [read_private_posts] => read_private_adoptions
    [create_posts] => edit_adoptions
)

Once I add 'map_meta_cap' => true, to your post type $args I get a much more complete object:

stdClass Object(
    [edit_post] => edit_adoption
    [read_post] => read_adoption
    [delete_post] => delete_adoption
    [edit_posts] => edit_adoptions
    [edit_others_posts] => edit_others_adoptions
    [publish_posts] => publish_adoptions
    [read_private_posts] => read_private_adoptions
    [read] => read
    [delete_posts] => delete_adoptions
    [delete_private_posts] => delete_private_adoptions
    [delete_published_posts] => delete_published_adoptions
    [delete_others_posts] => delete_others_adoptions
    [edit_private_posts] => edit_private_adoptions
    [edit_published_posts] => edit_published_adoptions
    [create_posts] => edit_adoptions
)
7
  • Thanks Nathan, forgot to add the 'capability_type' => array('adoption', 'adoptions'), 'map_meta_cap' => true, however i am still having an issue. the "adoption_agency" user is able to edit admin and shop_manager posts. do you have an idea why?
    – steamfunk
    Commented Mar 22, 2016 at 14:25
  • Ok, yes. You will want to then REMOVE the map_meta_cap line, and custom map like explained here: justintadlock.com/archives/2010/07/10/… Commented Mar 22, 2016 at 14:28
  • Basically, this allows the capability only when the specific post type is being manipulated. Hope that makes sense, and you get to your goal. Commented Mar 22, 2016 at 14:39
  • so basically i would have to do my own mapping. I have removed the line 'map_meta_cap' => true, and added the add_filter( 'map_meta_cap... but it is still not working. It is still allowing the adoption_agency user to have full control.
    – steamfunk
    Commented Mar 22, 2016 at 14:44
  • You will want to be sure to edit the adoption_agency role capabilities appropriately. Commented Mar 22, 2016 at 14:47

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.