I'm working on a WordPress website, and I'd like to implement a confirmation alert that appears before publishing a post. The goal is to prevent accidental publishing and allow users to confirm their intention to publish a post. I get the confirmation dialog box , but it saves the post doesn't matter if I select cancel or OK .
This is the code I am using inside functions.php
function add_confirmation_script() {
global $post_type;
// Check if we're in the post editor and it's not a new post
if ('post' === $post_type ) {
?>
<script>
jQuery(document).ready(function($) {
$(document).on('click', '.editor-post-publish-button', function(event) {
if (!confirm('Are you sure you want to publish this?')) {
event.preventDefault(); // Prevent the default action (post publish)
}
});
});
</script>
<?php
}
}
add_action('admin_footer', 'add_confirmation_script');