How can I add a jQuery OnClick event to the Publish posts button?

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:

Read More
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.

Related posts

Leave a Reply

2 comments

  1. 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: