0

I am trying to find a way/plugin where the posts of a contributor can only be edited or published by a specific user editor.

Like for example, if I have a contributor named Steve and an editor named Bob, Steve's posts/draft should only be reviewed and published by Bob who is assigned to the contributor. No other editors (except admin of course) can "touch" the work of Steve, only Bob can.

Is this possible? I tried the plug-in Members but I can only restrict the review and publishing of a contributor's post to a user role and not to a specific user.

1 Answer 1

0

You don't say how you save the editor -> contributor associations, I assume you store it in a user meta field with key 'assigned_contrib' where you save an array of contributors user id.

First off all, hide posts to other editors using pre_get_posts hook:

add_action('pre_get_posts', 'hide_posts_to_editors');

function hide_posts_to_editors( $query ) {
  if ( is_admin() && ! current_user_can('administrator') ) {
    $self = wp_get_current_user();
    $assigned_contributors = (array) get_user_meta($self->ID, 'assigned_contrib', true);
    $authors = array_merge($self->ID, $assigned_contributors);
    $query->set( 'author', implode(',', $authors) );
  }
}

In this way an editor can only see in admin posts written by himself/herself and the ones by the assigned editors.

But this not prevent and editor that know the post id to edit it by manual inserting the id in the address bar... you can use the load_{$page} action hook for the scope, in this case ('load-post.php')

add_action('load-post.php', 'no_editors_edit', 1);

function no_editors_edit() {
  if ( current_user_can('administrator') ) return;
  $post = isset($_GET['post']) && ! empty($_GET['post']) ? $_GET['post'] : false;
  if ( ! $post ) return;
  $author = get_post_field('post_author', $post);
  $self = wp_get_current_user();
  $assigned_contributors = (array) get_user_meta($self->ID, 'assigned_contrib', true);
  if ( ( $author != $self->ID ) && ! in_array($author, $assigned_contributors) ) {
     wp_redirect( add_query_arg( array('edit_not_allowed'=>1), admin_url() )  );
     exit();
  }
}

And add a notice if this happen...

add_action('admin_notices', 'no_editors_edit_notice');

no_editors_edit_notice() {
  if ( isset($_GET['edit_not_allowed'] && $_GET['edit_not_allowed'] == 1) ) {
    printf('<div class="error"><p>%s</p></div>', __('You are allowed to edit only your posts and the ones by contributors assigned to you.', 'your_text_domain'));
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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