Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type guest_blog registered with 'capability_type' => 'guest_blog' and 'map_meta_cap' => true. I want to restrict users that do not have publish_guest_blogs capability from editing a guest_blog with pending status. Here is the code I was using which did not work:

add_filter( 'user_has_cap', 'wpse_user_has_cap', 100, 3 );
function wpse_user_has_cap( $allcaps, $caps, $args ) {
    if( in_array( $args[0], array('edit_guest_blogs', 'delete_guest_blogs') )
        && empty( $allcaps['publish_guest_blogs'] ) //user can not publish guest blogs
        && ! empty( $allcaps['edit_guest_blogs'] ) ) //user can edit guest blog
    {
        // Load the post data:
        $post = get_post( $args[2] );

        if( $post->post_status == 'pending' )
              $allcaps[$caps[0]] = FALSE;
    }

    return $allcaps;
}

I wrote this based on the example in Codex (user_has_cap) and some answers here on WPSE (1, 2).

Then I found that $args[0] is asking for edit_posts and not edit_guest_blogs although I have 'map_meta_cap' => true and $caps[0] is the one that has the actual required capability (edit_guest_blogs). So I changed the first line to check against $caps[0] instead of $args[0] and then it worked:

//...
if( in_array( $caps[0], array('edit_guest_blogs', 'delete_guest_blogs') )

The question is if this is the right way to do it or not, and why all the samples are using $args[0] and not $caps[0]? What is the difference between these two parameters?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.