我已经设置了一个具有自定义功能的CPT。
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);
}
然后将这些能力分配给
administrator
和
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\');
到目前为止一切都很好,我可以看到
$wp_roles
设置所有自定义上限的全局数组
true
对于
administrator
和
subscriber
.
BUT
当我打电话的时候
current_user_can(\'delete_product\', $post_id);
它返回
false
. 我仔细检查了$post\\u id。它是由
$current_user
我花了几个小时试图找出这个问题。但不确定发生了什么。非常感谢您对故障排除的任何建议!
最合适的回答,由SO网友:Sisir 整理而成
Solved!
元功能
edit_product
,
delete_product
, \'read\\u product\'etc应单独处理。以下代码来自
Justin Tadlocks Siteadd_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;
}