Change published to pending after update

Change Published to Pending


Published to Pending

What to do if you wish to approve all changes made into published post by their authors? It’s possible by changing post status from ‘Published’ to ‘Pending Review’ automatically. You need just add a piece of code placed below to your current theme functions.php file. It works for users with ‘Author’ role. If you wish to change it, then change ‘author’ in the code to role name you wish to use.
Is that all you need to do? No. You should remove ‘publish_posts’ capability from your users, otherwise user can to publish pending post himself again. Use User Role Editor plugin for that.
Do you wish to see how does this hack work? Let’s proceed with short video demonstration.

Code you should use to achieve shown effect is

function published_to_pending($post_id) {
  global $post;
 
  if (!is_object($post)) {
    return;
  }
 
  if (current_user_can('author') && $post->post_status=='publish') {
    // stop recursion call
    remove_action('save_post', 'published_to_pending');     
    // update the post, which calls save_post again
    wp_update_post(array('ID' => $post_id, 'post_status' => 'pending'));
    // re-hook this function back
    add_action('save_post', 'published_to_pending');
  }
 
}
add_action('save_post', 'published_to_pending');
  • http://twitter.com/TheFrosty Austin ☃ Passy

    Had some issues with the above code. So I modified it to work for my needs:

    function extendd_change_published_to_pending( $post_id ) {
    global $post, $current_user;

    if ( !is_object( $post ) )
    $post = get_post( $post_id );

    if ( !current_user_can( ‘edit_post’, $post_id ) )
    return $post_id;

    if ( !current_user_can( ‘manage_shop_settings’ ) && ‘publish’ != $post->post_status ) {

    // unhook this function so it doesn’t loop infinitely

    remove_action( ‘save_post’, ‘extendd_change_published_to_pending’ );

    // Notify user
    delete_user_meta( $current_user->ID, ‘extendd_remove_vendor_pending_change_notice’ );

    // update the post, which calls save_post again
    wp_update_post( array( ‘ID’ => $post_id, ‘post_status’ => ‘pending’ ) );

    // re-hook this function back
    add_action( ‘save_post’, ‘extendd_change_published_to_pending’ );
    }

    }
    add_action( ‘save_post’, ‘extendd_change_published_to_pending’ );

    Hope it helps someone else.

  • http://shinephp.com Vladimir Garagulya

    Thanks for your contribution.

  • Dave

    Thank you for the superfast answer.
    It doesnt quite work yet. I tried the code above and also Austins edit. I get the same result. Error line at the top which doesnt disappear. and quite a messy error page when i try to log out Maybe i do something wrong im not a pro programmer.

  • http://shinephp.com Vladimir Garagulya

    According to screenshot you copied just part of Austin’s code, which is visible on the page by default. The rest part will be shown, if you click on “see more” link at the bottom of code fragment. That’s why you got error message.

    I tested code above again at my test site with WordPress 3.5.1 installed. It works as expected. You may download ready to execute file from this link
    http://shinephp.com/wp-content/downloads/wordpress/publish-to-pending.zip
    This code expected role with name ‘editor_without_publish’. What I did step by step:
    1) Using User Role Editor plguin I created new role with name ‘editor_without_publish’ as copy of the ‘editor’ role;
    2) excluded ‘publish_posts’ and ‘publish_pages’ capabilities from new created role ‘editor_without_publish’.
    3) assigned role ‘editor_without_publish’ to user I selected for testing.
    4) opened for editing post from other user, changed its content and clicked ‘Update’ button.

    As the result updated post got state ‘Pending Review’.

    1) You can place code from the file I mentioned into your functions.php file
    2) You can create directory wp-content/mu-plugins and place that file into this directory. It will be executed automatically by WordPress as plugin. ‘mu’ means ‘must use’ here.

  • jpfotoz

    Hi Vladimir. — I tried your code and it also didn’t work for me.

    I
    placed it in my theme’s function.php (it had some code already there so
    I added it at the end before the closing ?> bracket )

    I followed your steps – one exception my role is called “ConentWriter” – so I changed that value in your sample code.

    When I tested, the ‘Update” button present – and the page got published. NOT what I was expecting.

    I even made a /mu-plugins folder and place the php function inside as a filename.php

    What did I do wrong?

    Also Austin’s code above is incomplete – there’s an open { where there seems like it should be a closing }.

  • http://shinephp.com Vladimir Garagulya

    Hi,
    problem could be that you inserted role name “ConentWriter” into code, but you should use role ID, that is lowercase ‘conentwriter’. Try it please.

  • jpfotoz

    Hi Vladimir,

    That did work… BUT – the posted article now becomes in a preview mode — and thus the page throws a “page not found” error. any SEO to that page now is invalid. Is there any way to have it keep the original version as visible and submit some sort of preview for review. Once that is approved, the new version can replace the old version? I’m trying to manage a team of editors that need to review / edit posts by former employees. Thanks.

  • http://shinephp.com Vladimir Garagulya

    Hi,
    There is no way to achieve this without a lot of PHP/WordPress API coding. It is a subject of separate WP plugin development. I can imagine this as user with custom role for such plugin without ‘edit_others_posts’ capability get list of posts via plugin, selects one, edits its copy and sends for review. Administrator, or supervisor, defined by plugin, get notice about pending posts updates. Thus real post could be replaced updated one only after Admin moderate it.

  • Donna

    Hello,
    Thank you for this code, it almost worked perfectly for me. A problem I am having though, is once a pending edit is submitted, I can’t get to the unedited published page? Does anyone else have this problem?

  • http://shinephp.com/ Vladimir Garagulya

    Hi,
    Yes, it is the standard WordPress behavior. Post in the ‘pending’ state is not available to the public. In order to realize other strategy (save updated post to the post copy, which will wait when the admin approve it and only then replace the original post) the special plugin is needed.

  • Donna

    OK thanks. So the only plugin is the Press Permit package? Thanks again for your time and efforts!