Add a simple JS Alert Box on Post Submission

Note that when post is update WordPress redirect you from /wp-admin/post.php to /wp-admin/post.php?post=41&action=edit which mean it loads twice. Thus what you will do on transition status hooks that will be skipped before the content is printed.

Solution:-

  • In transition hook callback function store (post meta) some flag to display js popup
  • Bind a new function on admin_head hook
  • Check if flag is there display the popup and reset flag.

Example:-

//Update post meta to display alert
function add_js_for_pending($post) {
    update_post_meta($post->ID, 'trigger_notice', TRUE);
}
add_action( 'draft_to_pending', 'add_js_for_pending' );
add_action( 'auto-draft_to_pending', 'add_js_for_pending' );
add_action('new_to_pending', 'add_js_for_pending');

//JS alert on submit product

function notify_me_for_pending() {
    global $post;
    $current_screen = get_current_screen();
    //Check if we need to display alert
    if ($current_screen->base == 'post' && get_post_meta($post->ID, 'trigger_notice', TRUE)) {
        $notice = __('Thank you for your submission. Your product will be available for purchase as soon as we have approved it!', 'pressive-child'); ?>
        <script type="text/javascript">
            <?php echo 'alert("'.$notice.'");'; ?>
        </script><?php
         delete_post_meta($post->ID, 'trigger_notice'); //Alert is done now remove it.
    }
}
add_action('admin_head', 'notify_me_for_pending');