Wordpress Role Custom Capability is set true but returns false Wordpress Role Custom Capability is set true but returns false wordpress wordpress

Wordpress Role Custom Capability is set true but returns false


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 calls map_meta_cap which when checking against meta capabilities expects an array but is only supplied a single value)


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


Try user_can(). You will need to pass the user ID as first parameter. You can get it through get_current_user_id().