1

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.

1 Answer 1

1

It really depends on whether you want to restrict these new roles to a particular post type. If you search for "workflow plugins" you can find several ready-made solutions. Some only let the new roles edit Pages, some allow them to edit any post type.

I built a site that needed users who could only impact a particular CPT. So for example, we had "Editors" for CPT1 who could only "submit for review" and then "Approvers" for CPT1 who could edit and publish. We also had "Editors" for CPT2 and "Approvers" for CPT2, and so on and so forth, so people could only make changes to their assigned CPT.

The first step there was to use map_meta_cap when registering the CPTs, which created a whole new set of permissions for each CPT. The second step was more complicated and a bit hacky. First, I added a noscript tag to the admin so anyone with JS turned off couldn't get around and edit or publish things. I removed Quick Edit, and disabled Autosave, so the only way to update CPTs was to go to the full editor screen with JS enabled.

From there, I hooked into admin_footer-post.php and checked permissions. If the current user could not publish or edit the particular CPT they were currently editing, I used jQuery to change the "Publish" button to say "Save Changes," and changed the form action to my own plugin URL. The plugin takes all the POST data and programmatically inserts a new post with a custom status, then fires an email to the "Approvers" for that CPT with instructions on how to review - it takes them to the revision comparison screen.

I don't like that it relies on JS and I don't like overriding Core publishing features, but so far, other than another plugin that basically does the same thing, that's the only way I've found to handle multiple custom workflows.

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.