0

I have setup a CPT with custom capabilities.

add_action('init', 'fac_product_posttype_init');
function fac_product_posttype_init(){
    $product_labels = array( ... );
    $caps = array(
            'publish_posts' => 'publish_products',
            'edit_posts' => 'edit_products',
            'delete_posts' => 'delete_products',
            'edit_published_posts' => 'edit_published_products',
            'delete_published_posts' => 'delete_published_products',
            'delete_others_posts' => 'delete_others_products',
            'edit_others_posts' => 'edit_others_products',
            'edit_post' => 'edit_product',
            'delete_post' => 'delete_product',
            'read_post' => 'read_product',
            'upload_files' => 'upload_files'
        );
    $args = array(
        'labels' => $product_labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'product',
        'capabilities' => $caps,
        'hierarchical' => false,
        'supports' => array('title','editor','thumbnail'),
        'has_archive' => 'products',
        'menu_position' => 5, 
        'show_in_nav_menus' => false
    ); 
    register_post_type('product',$args);
}

Then assigned those capabilities to administrator and subscriber

function fs_add_roles(){
    $roles_set = get_option('fs_roles_are_set');
    //if(!$roles_set){

    $admin = get_role('administrator');
    $admin->add_cap('publish_products');
    $admin->add_cap('edit_products');
    $admin->add_cap('delete_products');
    $admin->add_cap('edit_published_products');
    $admin->add_cap('delete_published_products');
    $admin->add_cap('edit_others_products');
    $admin->add_cap('delete_others_products');

    //update_option('fs_roles_are_set', true);

    $subscriber = get_role('subscriber');
    $subscriber->add_cap('publish_products');
    $subscriber->add_cap('edit_products');
    $subscriber->add_cap('delete_products');
    $subscriber->add_cap('edit_published_products');
    $subscriber->add_cap('delete_published_products');
    //}
}
add_action('init', 'fs_add_roles');

All good upto this point and I can see on $wp_roles global array that all those custom caps are set true for administrator and subscriber.

BUT

when I call current_user_can('delete_product', $post_id); it returns false. I have double checked the $post_id. Its created by the $current_user

I am trying to figure out the problem for hours. But not sure whats going on. Any advice on troubleshooting is greatly appreciated!

2

Solved!

Meta Capabilities edit_product, delete_product, 'read_product` etc should be handled separately. Below code is from Justin Tadlocks Site

add_filter( 'map_meta_cap', 'fac_map_meta_cap', 10, 4 );

function fac_map_meta_cap( $caps, $cap, $user_id, $args ) {

    /* If editing, deleting, or reading a product, get the post and post type object. */
    if ( 'edit_product' == $cap || 'delete_product' == $cap || 'read_product' == $cap ) {
        $post = get_post( $args[0] );
        $post_type = get_post_type_object( $post->post_type );

        /* Set an empty array for the caps. */
        $caps = array();
    }

    /* If editing a product, assign the required capability. */
    if ( 'edit_product' == $cap ) {
        if ( $user_id == $post->post_author )
            $caps[] = $post_type->cap->edit_posts;
        else
            $caps[] = $post_type->cap->edit_others_posts;
    }

    /* If deleting a product, assign the required capability. */
    elseif ( 'delete_product' == $cap ) {
        if ( $user_id == $post->post_author )
            $caps[] = $post_type->cap->delete_posts;
        else
            $caps[] = $post_type->cap->delete_others_posts;
    }

    /* If reading a private product, assign the required capability. */
    elseif ( 'read_product' == $cap ) {

        if ( 'private' != $post->post_status )
            $caps[] = 'read';
        elseif ( $user_id == $post->post_author )
            $caps[] = 'read';
        else
            $caps[] = $post_type->cap->read_private_posts;
    }

    /* Return the capabilities required by the user. */
    return $caps;
}

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.