The functionality I'm trying to achieve is a user role that allows the user to submit drafts of edits but not publish edits. Their edits must be published by an Editor or higher role user.
I've searched around this Stack Exchange a bit and I haven't found any solutions that seem to work for my needs. I would prefer to avoid plugins and build this functionality directly into my functions.php file.
I'm working in a non-live sandbox environment so I have no major concern of breaking anything while testing out different solutions. So far this is what I have:
$role_object = get_role( 'contributor' );
$role_object->add_cap( 'edit_pages' );
$role_object->add_cap( 'edit_others_pages' );
$role_object->add_cap( 'edit_published_pages' );
$role_object->add_cap( 'upload_files' );
$role_object->remove_cap( 'publish_posts' );
$role_object->remove_cap( 'publish_pages' );
add_filter('wp_insert_post_data','re_aprove');
function re_aprove($data , $postarr){
global $current_user;
get_currentuserinfo();
//check if current user is not admin
if (!current_user_can('manage_options')){
if ($data['post_status'] == "publish"){
$data['post_status'] = "pending";
}
}
return $data;
}
The second function was found on here but I suspect it might be a deprecated solution as it doesn't seem to be working correctly for me and I found a post containing it from 2010.
Its the way in which this solution isn't working that has me scratching my head. As a Contributor I can go to the editing page for any page, however I'm presented with an "Update" button instead of a "Submit for Review" button. Upon pressing the "Update" button the editing page refreshes and shows a "Submit for Review" button instead and the Pages dashboard page shows the updated page as "Pending." So far so good for the most part. The problem is that the page now 404s on the front-end. 'Updating' it as a contributor has unpublished the page which is not what I was going for.
Any thoughts, suggestions, or solutions are greatly appreciated. I think this functionality would be pretty desirable to many people.
Thanks for your time.