Reputation: 776
I try to create a user role with some custom capabilities. That works so far. But if I want to check the user permissions of one specific capability with the function current_user_can()
it returns false. But inside the capabilities array of the new created role the specific capability is set to true.
So to give my words a little bit more coding background:
THE ROLE OBJECT
WP_Role {
["name"]=> "organizer"
["capabilities"]=> {
["edit_posts"] => bool(false)
["delete_posts"] => bool(false)
["publish_posts"] => bool(false)
["upload_files"] => bool(true)
["edit_event"] => bool(true)
["read_event"] => bool(true)
["delete_event"] => bool(true)
["edit_events"] => bool(true)
["edit_others_events"] => bool(false)
["publish_events"] => bool(false)
["read_private_events"] => bool(true)
["read"] => bool(true)
["delete_events"] => bool(true)
["delete_private_events"] => bool(false)
["delete_published_events"] => bool(true)
["delete_others_events"] => bool(false)
["edit_private_events"] => bool(false)
["edit_published_events"] => bool(true)
["manage_event_terms"] => bool(true)
["edit_event_terms"] => bool(true)
["delete_event_terms"] => bool(true)
["assign_event_terms"] => bool(true)
}
MY ADD ROLE FUNCTION
add_role( 'organizer', __( 'Organizer', 'eventtool' ), array(
// General
'edit_posts' => false,
'delete_posts' => false,
'publish_posts' => false,
'upload_files' => true,
'edit_event' => true,
'read_event' => true,
'delete_event' => true,
// Primitive capabilities used outside of map_meta_cap():
'edit_events' => true,
'edit_others_events' => false,
'publish_events' => false,
'read_private_events' => true,
// Primitive capabilities used within map_meta_cap():
'read' => true,
'delete_events' => true,
'delete_private_events' => false,
'delete_published_events' => true,
'delete_others_events' => false,
'edit_private_events' => false,
'edit_published_events' => true,
'edit_events' => true,
// Terms
'manage_event_terms' => true,
'edit_event_terms' => true,
'delete_event_terms' => true,
'assign_event_terms' => true
)
);
CUSTOM POST TYPE ARGS
register_post_type( 'event', array(
'labels' => $labels,
'description' => __( 'This is where you can add new events to your page.', 'eventtool' ),
'public' => true,
'show_ui' => true,
'capability_type' => 'event',
'map_meta_cap' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'hierarchical' => false,
'rewrite' => _x( 'event', 'slug', 'eventtool' ),
'query_var' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'show_in_nav_menus' => true
)
);
[UPDATE]
function et_modify_map_meta_cap( $caps, $cap, $user_id, $args ) {
var_dump($cap)
}
add_filter( 'map_meta_cap', 'et_modify_map_meta_cap', 10, 4 );
Outputs 'edit_post' instead of 'edit_event'
Any suggestions, why this wrong behaviour comes up?
Upvotes: 14
Views: 2633
Reputation: 776
Today I solved it.
It is neccessary to assign an object id for the second parameter to the current_user_can()
function to get the correct return of single pointed capabilities (eg. 'edit_post'). Otherwise the function will return false, if this parameter is not set.
As inside the WordPress Documentation:
If omitted you may receive an 'Undefined offset: 0' warning (this is because the
current_user_can
function eventually callsmap_meta_cap
which when checking against meta capabilities expects an array but is only supplied a single value)
Upvotes: 1
Reputation: 1967
Try user_can(). You will need to pass the user ID as first parameter. You can get it through get_current_user_id().
Upvotes: 0
Reputation: 2398
Try to register these capabilities
to the custom post type event
first , and then assign those capabilities
to organiser
Like this :
function create_my_post_types() {
register_post_type(
'movie',
array(
'public' => true,
'capability_type' => 'movie',
'capabilities' => array(
'publish_posts' => 'publish_movies',
'edit_posts' => 'edit_movies',
'edit_others_posts' => 'edit_others_movies',
'delete_posts' => 'delete_movies',
'delete_others_posts' => 'delete_others_movies',
'read_private_posts' => 'read_private_movies',
'edit_post' => 'edit_movie',
'delete_post' => 'delete_movie',
'read_post' => 'read_movie',
),
)
);
}
Change the code to event
post type.
And check the user capababilities by below code instead :
add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 );
function my_map_meta_cap( $caps, $cap, $user_id, $args ) {
/* If editing, deleting, or reading a movie, get the post and post type object. */
if ( 'edit_movie' == $cap || 'delete_movie' == $cap || 'read_movie' == $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 movie, assign the required capability. */
if ( 'edit_movie' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
}
/* If deleting a movie, assign the required capability. */
elseif ( 'delete_movie' == $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 movie, assign the required capability. */
elseif ( 'read_movie' == $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;
}
Upvotes: 0