Support

Account

Home Forums Backend Issues (wp-admin) Execute JS after validate_save_post

Unread

Execute JS after validate_save_post

  • Hello.

    I’m trying to implement a confirm box when an author want to publish a post. On click on #publish input, a JS confirm box appears and he has to click on « ok ». Basic.

    Code of this part (maybe perfectible):

    
    add_action('admin_footer', 'confirmPublish');
    
    function confirmPublish() {
        global $post_type;
        if (isset($post_type) && $post_type == 'post') { ?>
    
        <script>
            var publish = document.getElementById('publish');
            console.log(publish);
            publish.addEventListener('click', function(event) {
                console.log('publish in progress...');
                var result = confirm('Voulez-vous vraiment publier cet article ?');
                if (!result) {
                    event.preventDefault();
                } else {
                    return true;
                }
            });
        </script>
    
        <?php }
    }
    

    The problem is: due to the acf_validate_save_post action, my JS is executed twice. One before the ACF action, and one after the hook, when the original publish click event is triggered again.

    What can I do to trigger my event only if the return of acf_validate_save_post is okay?

    thanks for any help.

Viewing 1 post (of 1 total)

The topic ‘Execute JS after validate_save_post’ is closed to new replies.