Benutzerdefinierte functionen für CPT und Problem mit current_user_can ()

Ich habe einen CPT mit benutzerdefinierten functionen eingerichtet.

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); } 

Diese functionen wurden dann dem administrator und subscriber zugewiesen

 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'); 

Alles gut bis zu diesem Punkt und ich kann auf $wp_roles globalen Array sehen, dass alle diese benutzerdefinierten Obergrenzen für administrator und subscriber true sind.

ABER

wenn ich current_user_can('delete_product', $post_id); es gibt false . Ich habe die $ post_id doppelt überprüft. Es wird von $current_user

Ich versuche das Problem stundenlang herauszufinden. Aber nicht sicher, was los ist. Jeder Rat zur Fehlerbehebung wird sehr geschätzt!

Solutions Collecting From Web of "Benutzerdefinierte functionen für CPT und Problem mit current_user_can ()"

Getriggers!

Meta-Fähigkeiten edit_product , delete_product , ‘read_product` usw. sollten separat behandelt werden. Der folgende Code stammt von 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; }