Add confirmation popup on “Move to Trash”

I want to open javascript message “Are you sure you want to move the post to trash?”when we clicks on “Move to Trash” link in Publish box.
and when we click to OK the post go to trash or cancel to go back to the post.

I found this code on previous question. It’s open the javascript message but when I click OK it’s just close the popup and don’t move the post to trash. What can I add to fix it?

CODE :

if (! empty($GLOBALS['pagenow']) && 'post.php' === $GLOBALS['pagenow'])
    add_action('admin_footer', 'trash_click_message');
function trash_click_message() {
    echo <<<JQUERY
<script>
    jQuery(function($) {
        $('#delete-action a').unbind();
        $('#delete-action a').click(function(event) {
            event.preventDefault();
            alert('Are you sure you want to move the post to trash?');
            setTimeout(
                function() {
                    $('#save-action .spinner').hide();
                    $('#publish').removeClass('button-primary-disabled');
                },
                1
            );
        });
    });
</script>
JQUERY;
}

Picture of my actuel result

enter image description here

UPDATE :

Thanks bergire! It’s all most done. I just have one issues. I use WordPress in english but the serveur is on France. So the confirmation popup is in french like you see in the picture.enter image description here

Is it a way to change labels to (indique) and (annuler) words?

1 Answer
1

You can try to admin enqueue this script, with jquery dependency:

jQuery( function($) {       
    $('.edit-php a.submitdelete, .post-php a.submitdelete').click( function( event ) {
        if( ! confirm( 'Are you sure you want to move the post to trash?' ) ) {
            event.preventDefault();
        }           
    });
});

where we restrict the conformation on the a.submitdelete clicks to the .edit-php and .post-php body classes.

Hope you can adjust it to your needs, e.g. regarding i18n.

Leave a Comment