4

I want to add a simple confirmation event to the Publish posts button, so when my client hits "Publish" it will ask him if he's sure, to which he clicks "Yes" or "cancel" and the post then publishes or doesn't.

I'm new to WordPress...or at least I've only done theme and limited plugin programming. I did find the metabox code for the "Publish" button in edit-form-advanced.php:

add_meta_box('submitdiv', __('Publish'), 'post_submit_meta_box', null, 'side', 'core');

But to accomplish this, I suspect I'll need to add the jQuery code elsewhere - preferably in my theme.

For site-specific reasons, I cannot add new plugins to this installation so any changes need to be confined to my theme's functions.php file.

EAMann
  • 32,177
  • 9
  • 88
  • 147
Lynn
  • 337
  • 3
  • 9

2 Answers2

3

You can hook into the post footer actions (based on this answer, not tested):

add_action( 'admin_footer-post-new.php', 'wpse_80215_script' );
add_action( 'admin_footer-post.php', 'wpse_80215_script' );

function wpse_80215_script()
{
    if ( 'post' !== $GLOBALS['post_type'] )
        return;

    ?>
<script>
document.getElementById("publish").onclick = function() {
    if ( confirm( "Ready?" ) )
        return true;

        return false;

}</script>
    <?php
}

These actions are called in wp-admin/admin-footer.php:

do_action( "admin_footer-" . $GLOBALS['hook_suffix'] );

This code can be used in a plugin (preferred) or in your theme’s functions.php.
See also:

fuxia
  • 107,219
  • 39
  • 255
  • 462
0

There's a WordPress plugin for that:

Confirm Publishing Actions is a WordPress plugin that prompts a user to click a confirm (or cancel) button whenever he is trying to submit, publish, update or delete a WordPress post.

brasofilo
  • 22,156
  • 8
  • 70
  • 270
diggy
  • 1,627
  • 1
  • 12
  • 10