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.

2
  • 2
    The number of plugins a site uses is irrelevant, it's what each plugin does that matters. The same solution added directly to your theme or via a plugin places an identical load on your site.
    – Milo
    Commented Jan 11, 2013 at 19:10
  • Yes. Well, I'm not here to debate the finer points of how many plugins do what, I'm just really interested in getting my question answered in such a way that circumvents my having to use a plugin to achieve my goal. I'm fine with adding code to the theme of this site...one of hundreds that I have on my multi site install.
    – Lynn
    Commented Jan 11, 2013 at 19:56

2 Answers 2

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:

6
  • Ok, so I added the above code into my functions.php file and nothing happened. Is it correct to assume the js script goes into the admin_footer file? In this case I first tried the code by adding the entire snippet above to functions.php...
    – Lynn
    Commented Jan 11, 2013 at 21:02
  • Yes, just add everything to the functions.php. I have tested it now, it works.
    – fuxia
    Commented Jan 11, 2013 at 21:06
  • also...one thing that's confusing to me...I don't see any files in wp-admin called admin_footer-post-new.php or admin_footer-post.php. Just admin-footer.php which seems like a pretty simple file. Would this cause my hooks not work?
    – Lynn
    Commented Jan 11, 2013 at 21:25
  • @Lynn See my update.
    – fuxia
    Commented Jan 11, 2013 at 21:29
  • Thanks. I see now. i was putting everything into functions. Informative links too.
    – Lynn
    Commented Jan 11, 2013 at 21:40
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.

1
  • Unfortunately, I'm in a position where I'm not allowed to add a plugin to the multi site installation. It's suffering greatly from plugin creep and it's just not an option.
    – Lynn
    Commented Jan 11, 2013 at 18:55

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.