Hello - my apologies for resurrecting a closed thread (https://toolset.com/forums/topic/capability_type/#post-38921).
I am trying to add capabilities to a post type created through types. My post type is product. Right now, when custom post types are created, the plugin sets capabilities = false, which means it defaults to post. But I would like a user to be able to edit the product post type but not the post post type.
Can you help me with the exact function I would need to add to accomplish this? I looked in that old answer, but line 210 in the file referenced didn't have that function, so it appears the plugin may have changed since then. Thank you!
Hi Price,
Are you using latest version of Types plugin to create post type "product"?
the type's filter hook 'wpcf_type' locate in custom-types.php line 187:
apply_filters( 'wpcf_type', $data, $post_type ) );
OK, I see that code, but still don't understand what the function I would add to functions.php would be to add the 'capability_type' => 'product'? I am trying to add things like:
add_filter( 'wpcf_type', 'ead_add_product_caps', 'product');
function ead_add_product_caps($args) {
$args = array(
'capability_type' => 'product',
'capabilities' => array(
'publish_posts' => 'publish_products',
'edit_posts' => 'edit_products',
'edit_others_posts' => 'edit_others_products',
'delete_posts' => 'delete_products',
'delete_others_posts' => 'delete_others_products',
'read_private_posts' => 'read_private_products',
'edit_post' => 'edit_product',
'delete_post' => 'delete_product',
'read_post' => 'read_product',
),
)
}
apply_filters( 'wpcf_type', 'ead_add_product_caps' , 'product' );
Nothing is working. Thanks.
Please try modify your codes like this:
add_filter( 'wpcf_type', 'ead_add_product_caps', 10, 2);
function ead_add_product_caps($data, $post_type) {
if($post_type == 'product'){
print_r($data);
// here add your custom codes
}
return $data;
}
More help:
http://codex.wordpress.org/Function_Reference/add_filter
Hi. OK after much testing, it appears that $data is the text of an array, you can't add to it in this way. When you print the array you see:
Array ( [wpcf-post-type] => product [labels] => Array ( [name] => Products [singular_name] => Product [add_new] => Add New [add_new_item] => Add New Product [edit_item] => Edit Product [new_item] => New Product [view_item] => View Product [search_items] => Search Products [not_found] => No Products found [not_found_in_trash] => No Products found in Trash [parent_item_colon] => Parent Product [all_items] => Products ) [slug] => product [description] => [public] => 1 [taxonomies] => Array ( [0] => seller [1] => wedding-colors [2] => shop [3] => color ) [supports] => Array ( [0] => title [1] => editor [2] => thumbnail [3] => custom-fields ) [rewrite] => Array ( [enabled] => 1 [with_front] => 1 [feeds] => 1 [pages] => 1 ) [has_archive] => 1 [show_in_menu] => 1 [show_in_menu_page] => [show_ui] => 1 [publicly_queryable] => 1 [can_export] => 1 [show_in_nav_menus] => 1 [query_var_enabled] => 1 [query_var] => 1 [permalink_epmask] => 1 [exclude_from_search] => [hierarchical] => )
Adding the capability type code just gives (see the end):
Array ( [wpcf-post-type] => product [labels] => Array ( [name] => Products [singular_name] => Product [add_new] => Add New [add_new_item] => Add New Product [edit_item] => Edit Product [new_item] => New Product [view_item] => View Product [search_items] => Search Products [not_found] => No Products found [not_found_in_trash] => No Products found in Trash [parent_item_colon] => Parent Product [all_items] => Products ) [slug] => product [description] => [public] => 1 [taxonomies] => Array ( [0] => seller [1] => wedding-colors [2] => shop [3] => color ) [supports] => Array ( [0] => title [1] => editor [2] => thumbnail [3] => custom-fields ) [rewrite] => Array ( [enabled] => 1 [with_front] => 1 [feeds] => 1 [pages] => 1 ) [has_archive] => 1 [show_in_menu] => 1 [show_in_menu_page] => [show_ui] => 1 [publicly_queryable] => 1 [can_export] => 1 [show_in_nav_menus] => 1 [query_var_enabled] => 1 [query_var] => 1 [permalink_epmask] => 1 [exclude_from_search] => [hierarchical] => ) [capability_type] => product
Since it's outside the parenthesis, it invalidates the post type and therefore doesn't even create it.
Other ideas? This really should just be an option on the creation page.
Thanks.
FYI, I have come a bit closer using this function:
function ead_mod_product( $post_type, $args ) {
// Make sure we're only editing the post type we want
if ( 'product' != $post_type )
return;
$args->capability_type = product;
// Modify post type object
$wp_post_types[$post_type] = $args;
}
add_action( 'registered_post_type', 'ead_mod_product', 10, 2 );
It does modify the capability_type but as the cap object was already set in the plugin creation, it doesn't modify the capabilities. Adding capabilities as an argument simply adds it into the end of the $args array, again, not modifying the object.
I suggest you try modify your codes like this:
add_filter( 'wpcf_type', 'ead_add_product_caps', 10, 2);
function ead_add_product_caps($data, $post_type) {
if($post_type == 'product'){
$args = array(
'capability_type' => 'product',
'capabilities' => array(
'publish_posts' => 'publish_products',
'edit_posts' => 'edit_products',
'edit_others_posts' => 'edit_others_products',
'delete_posts' => 'delete_products',
'delete_others_posts' => 'delete_others_products',
'read_private_posts' => 'read_private_products',
'edit_post' => 'edit_product',
'delete_post' => 'delete_product',
'read_post' => 'read_product',
),
);
$data = array_merge($data, $args);
}
return $data;
}
Thank you so, SO much! This worked! If anyone else is looking at this later on, you will then need to add the capabilities to each role, using something like the Members plugin.
Just FYI - This is the code I actually used to make this work properly for me.
add_filter( 'wpcf_type', 'qd_add_listing_caps', 10, 2);
function qd_add_listing_caps($data, $post_type) {
if($post_type == 'listing'){
$args = array(
'capability_type' => 'listing',
'capabilities' => array(
'edit_post' => 'edit_listing',
'read_post' => 'read_listing',
'delete_post' => 'delete_listing',
'edit_posts' => 'edit_listings',
'edit_others_posts' => 'edit_others_listings',
'publish_posts' => 'publish_listings',
'read_private_posts' => 'read_private_listings',
'delete_posts' => 'delete_listings',
'delete_private_posts' => 'delete_private_listings',
'delete_published_posts' => 'delete_published_listings',
'delete_others_posts' => 'delete_others_listings',
'edit_private_posts' => 'edit_private_listings',
'edit_published_posts' => 'edit_published_listings'
),
'map_meta_cap' => true
);
$data = array_merge($data, $args);
}
return $data;
}
function add_theme_caps() {
// gets the admin role
$role = get_role( 'administrator' );
// Give admin full access to the new post type
$role->add_cap( 'edit_listing' );
$role->add_cap( 'read_listing' );
$role->add_cap( 'delete_listing' );
$role->add_cap( 'edit_listings' );
$role->add_cap( 'edit_others_listings' );
$role->add_cap( 'publish_listings' );
$role->add_cap( 'read_private_listings' );
$role->add_cap( 'delete_listings' );
$role->add_cap( 'delete_private_listings' );
$role->add_cap( 'delete_published_listings' );
$role->add_cap( 'delete_others_listings' );
$role->add_cap( 'edit_private_listings' );
$role->add_cap( 'edit_published_listings' );
}
add_action( 'admin_init', 'add_theme_caps');