0

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?

-3

There's another great article here. They walk you through the 4-step process that they used to set it up.

  • 1
    Lone link is considered a poor answer (see help > deleted answers) since it is meaningless by itself and target resource is not guaranteed to be alive in the future. Please try to include at least summary of information you are linking to. – Rarst Sep 30 '15 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.