2

Contributors on our wordpress site can't preview posts. I'd like them to be able to preview any post (not just theirs) and this should include custom post types.

I can't find a specific capability to add to the contributor role like I've done with other capabilities.

2

Unfortunately, WordPress doesn't use a separate capability for previewing posts. A user needs the ability to edit a post in order to preview it. As seen in the WP_Query::get_posts() method:

// User must have edit permissions on the draft to preview.
if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
    $this->posts = array();
}

What you could do is use a combination of the posts_results filter (which is applied before unpublished posts are handled) and the the_posts filter (which is applied after), to re-populate the posts array if the user is a Contributor.

Please double-check this for any security implications of allowing lower level users access to preview unpublished content.

Untested example:

class wpse_196050 {

    protected $posts = array();

    public function __construct() {
        add_filter( 'posts_results', array( $this, 'filter_posts_results' ), 10, 2 );
        add_filter( 'the_posts',     array( $this, 'filter_the_posts' ), 10, 2 );
    }

    public function filter_posts_results( array $posts, WP_Query $query ) {
        if ( $query->is_preview ) {
            $this->posts = $posts;
        }
        return $posts;
    }

    public function filter_the_posts( array $posts, WP_Query $query ) {
        if ( ! empty( $this->posts ) && current_user_can( 'edit_posts' ) ) {
            $posts = $this->posts;
        }
        return $posts;
    }

}

new wpse_196050;
1

Using a custom plugin, and based off this answer:

//allow post preview if you are the post owner, whatever role you might have (e.g. contributor)
function jv_change_post( $posts ) {
    if(is_preview() && !empty($posts)){
        if(user_can('contributor')) 
            $posts[0]->post_status = 'publish';
    }

    return $posts;
}
add_filter( 'posts_results', 'jv_change_post', 10, 2 );

This basically makes 'future' posts pretend like they're already published (but only in the context of the current query), which makes it possible for the contributor to view them.

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.