0

I am trying to make any post that has been tagged and saved with a certain status non-editable by editor users.

I am using a custom plugin that extends the status' to include subbing, however, at present I am just trying to get the functionality there so am using the publish status.

Can anyone see what's wrong with my code below?

As far as i can tell, I am assigning publish status and editor user to variables, checking if these are true and then removing editing capabilities, then hooking this in before the post is published.

// Restrict editors making changes to articles after tagged published
function restrict($new_status, $post){
    $pub = get_post_status($post) == 'publish';
    $editor = get_role('editor');
    if ($new_status == $pub && (current_user_can('edit_post'))){
        remove_cap($editor, 'edit_posts');
    }
}
add_action('publish_post', 'restrict', 10, 2);
5

1 Answer 1

1

Don't remove the capability - this will make all editors unable to edit all posts. Instead, use a filter to conditionally determine if the post can be edited:

function wpse_187738_map_meta_cap( $caps, $cap, $user_ID, $args ) {
    if ( $cap === 'edit_post' && $args && ! current_user_can( 'manage_options' ) /** Only proceed for non-administrators */ ) {
        $post_id = $args[0];
        if ( has_tag( 'tag_slug_or_id', $post_id ) )
            $caps[] = 'not_allowed';
    }

    return $caps;
}

add_filter( 'map_meta_cap', 'wpse_187738_map_meta_cap', 10, 4 );
2
  • Thanks a lot for your quick answer! Would you mind just explaining the part in the conditional where $args is parsed without being assigned anything? I'm still very much learning PHP/wordpress structure
    – user657489
    May 8, 2015 at 13:35
  • Read up on filters. The arguments are passed to the function from the filter caller, in this case map_meta_cap May 8, 2015 at 13:37

Your Answer

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

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