Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

A very common issue that I experience with new authors is that they forget to complete a few essential tasks prior to submitting their post for review (even though I have a guide available for them).

Is it possible to add a confirmation box that appear only when a contributor clicks on the "Submit for Review" button? Potentially a jQuery box like this:

enter image description here

I have come across a few confirm publish codes on the web and plugin archives but all of which trigger on Publish (not Submit for Review) and prompts a Javascript alert window.

share|improve this question

2 Answers 2

up vote 9 down vote accepted
+50

A useful fast way is use javascript in combination with an ajax action that output the box. Create a small plugin. Create a folder inside plugins directory, naming it 'SubmitReviewConf'. In this folder create a php file named SubmitReviewConf.php.

in this file, put this code:

<?php
/**
* Plugin Name: Submit for Review Confirmation Message
* Description: Confirmation Message for Submit for Review Action
* Author: G.M.
*/

function add_my_admin_assets( $hook ) {
  // Only once variable force the box to be shown only one time. Can be from theme or plugin via filter.
  // Default is true
  $only_once = apply_filters('submit_review_conf_onlyonce', 1);
  //
  if ( $only_once && $hook == 'post.php' ) $post = get_post( $_GET['post'] );
  $enqueue = $only_once ? ($hook == 'post-new.php' || ( $hook == 'post.php' && $post->post_status != 'pending') ) : ($hook == 'post-new.php' || $hook == 'post.php');
  if ( isset($post) && get_post_meta( $post->ID, '_skip_SubmitReviewConf', true ) ) return;
  $postid = '';
  if ( isset($_GET['post']) && is_object($post) ) $postid = $post->ID;
  if ( $enqueue && ! current_user_can('edit_published_posts')) {
    wp_enqueue_style('thickbox');
    wp_enqueue_script('SubmitReviewConf', plugins_url( 'SubmitReviewConf.js', __FILE__ ), array('jquery','thickbox'), null );
    wp_localize_script('SubmitReviewConf', 'SubmitReviewConfData', array( 'only_once' => $only_once, 'postid' => $postid ) );
  } 
}
add_action( 'admin_enqueue_scripts', 'add_my_admin_assets', 30 );


function clean_up_SubmitReviewConf_meta( $post ) {
     delete_post_meta( $post->ID, '_skip_SubmitReviewConf');
}
add_action( 'pending_to_publish', 'clean_up_SubmitReviewConf_meta', 30 );      

function confirmation_msg_out() {
    if ( isset($_GET['only_once']) && isset($_GET['postid']) ) update_post_meta( $_GET['postid'], '_skip_SubmitReviewConf', '1' );
    // SET HTML FOR THE CONFIRMATION MESSAGE IN THIS FUNCTION
?>
  <div id="confirmation_msg_out">
  <ul>
    <li><?php _e('Did you ... '); ?></li>
    <li><?php _e('Did you ... '); ?></li>
    <li><?php _e('Did you ... '); ?></li>
  </ul>
  <input id="confirmation_msg_submit" class="button button-primary button-large" value="<?php _e('Submit for Review') ?>" name="submitforreview"></input>
  <a id="confirmation_msg_submit_cancel" class="button button-large" href="#"><?php _e('Cancel') ?></a>
  </div>
<?php
  die();
}
add_action( 'wp_ajax_output_review_confirm', 'confirmation_msg_out' );

This small plugin contain only 2 functions. ( Edit: after an OP request in comments become 3)

The first enqueue a custom script (what is it is explained later) and the 'thicbox' script (the script wordpress use to show modal messages).

This scripts are enqueued only if current user cannot edit published posts, so authors and higher users can publish posts with no confirmation message.

The second function output the html for the confirmation box. This function is hooked into an ajax action, output_review_confirm.

Now in the same folder of php file let's create our javascript file and name it SubmitReviewConf.js.

In this file put:

jQuery().ready( function($) {

var $publish = $('#publish');

var only_once = SubmitReviewConfData.only_once;

function remove_confirm_publish_button() {
  $('#confirm-publish-button').remove();
  $publish.show();
}

$(document).on('click', '#confirm-publish-button', function(e) {
  e.preventDefault();
  if ( only_once ) remove_confirm_publish_button();
  var tb_show_url = ajaxurl + '?action=output_review_confirm';
  if (only_once) tb_show_url += "&only_once=1";
  if ( SubmitReviewConfData.postid != '' ) tb_show_url += "&postid=" +  SubmitReviewConfData.postid;
  tb_show('', tb_show_url);
});

$(document).on('click', '#confirmation_msg_submit', function(e) {
  e.preventDefault();
  tb_remove();
  $publish.click();
});

$(document).on('click', '#confirmation_msg_submit_cancel', function(e) {
  e.preventDefault();
  tb_remove();
});

var newbutton = '<input id="confirm-publish-button" class="button button-primary button-large" type="button" value="' + $publish.val() + '"></input>';
$publish.hide().after(newbutton);

});

What this script do is:

  1. Hide the wordpress 'Submit for Review' button and replace it with a button that appera equel to the wp one, but that do not trigger the submit action.
  2. When out fake submit button is clicked, we use the ajax url to trigger our function and output the html inside the modal confirmation, using the tb_show function provided by thickbox script.
  3. When the use click for 'Submit for Review' inside the modal box, we simulate a click on the wp standard 'Submit for Review' button and remove the modal window. If the user click on 'Cancel' we simply remove the modal window doing nothing.

That's all. Remember to activate the plugin in WP backend ;)

Note that this plugin require javascript is enabled on user browser. If disabled the user will not see any confirmation, but consider that modal window (thickbox) is a javascript script that cannot work if javascript is disabled.

If this feature was a critical one, you probably wanted to disable publishing at all if js is disabled, but being not-critical feature you can just ignore user that have js disabled (wordpress will take care for your security, in that case) considering they are a very small percentage of users.

share|improve this answer
    
Before I praise you for this solution, let us make it work accordingly. Currently upon plugin activation, this error appears: Parse error: syntax error, unexpected '}' in /SubmitReviewConf/SubmitReviewConf.php on line 30 –  Christine Cooper Aug 28 '13 at 15:51
    
Sworn that I did that, at least when I pasted it in phpStorm. Nonetheless, this is a stellar answer and works very elegantly. Very well done! I will leave the question open for a few days so the answer receives more attention (upvotes) which I certainly believe that it deserves. With that being said, I do have a final question. Like many other multi-author platforms, I use WyPiekacz wordpress.org/plugins/wypiekacz which is a plugin where you can set conditions that users need to follow when posting. –  Christine Cooper Aug 29 '13 at 14:07
    
Problem is that the broken rule notification appears after the thickbox appears. Making this a very painful task if the user have to see the thickbox several times- which could potentially make this an annoyance rather than a benefit. Is there any way to enforce these rules first and when they are passed, then display the thickbox? –  Christine Cooper Aug 29 '13 at 14:08
    
@ChristineCooper The problem is that your plugin check rules on wp_insert_post_data filter. So if post pass rules, post is saved very soon, and I think there is no chance to show the modal box before post is saved as 'pending'... An easy edit I can make to my plugin is to force modal box appear only one time, but still before rules are passed or after post is already sended for review. –  G. M. Aug 29 '13 at 15:20
1  
@ChristineCooper "Do you have a fix for this?" Is this a challenge? ;D Solve bug was very easy for itself, problem is compatibility with WyPiekacz. Only for that I have to implement additional code handling a meta field. Btw now bug should be fixed in WyPiekacz compatible way. Only check this workflow: (I haven't WyPiekacz to test): create and save post as draft. After saved change it in a way that make WyPiekacz rules fail. Send for review, modal should appear. In modal click 'Submit' this will trigger WyPiekacz error. Correct it and send for review again. Does modal appear again? –  G. M. Aug 30 '13 at 18:13

It is possible to do so. You have to use the wp_insert_post_data filter hook. I will give you the general idea, hopefully this helps.

An example would be:

function my_filter_handler($data , $postarr) {
    // do something with the post data

    // You have to check that your post is submitted for review, not the publish post, so you have to have an if statement checking that

    // Then, you have to ensure that you use jQuery that comes with WordPress (using enqueue methods), and not a jQuery you supply.

    return $data;
}

add_filter('wp_insert_post_data', 'my_filter_handler', '99', 2 );
share|improve this answer
    
By the way, another approach would be to use the 'save_post' action hook instead of the wp_insert_post_data filter hook. Just so you have different ideas. –  Greeso Aug 12 '13 at 17:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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