0

I would like jQuery to click on a button/link after a Wordpress post is successfully published for the first time. Here are CSS selectors for WP publish ID:

    // Before a post is published:
    <input type="submit" name="publish" id="publish" class="button button-primary button-large preventDefault" value="Publish">

    // After a post is published:
    // <input type="submit" name="save" id="publish" class="button button-primary button-large preventDefault" value="Update">

#publish has different values before and after WP publishes a post. Here are my test codes:

    document.getElementById("publish").onclick = function () {
        var isPublished = this.value;
        if (isPublished === 'Update') {         // works with Publish before post published; not work with Update after post published
          alert('A new post has been successfully published');
        }
        //  return true;

        //return false;

      }

The above codes dont work because var isPublished = this.value; obtains a value before WP publishes a new post. How can I get that value after a new post is successfully published.

Or is there another way of checking is a WP post is successfully posted?

Very appreciate any help.

2
  • Why would a post not be successfully published? Is that all you want to know, or do you plan on doing something else when a post is published? If it's the latter then in the modern editor you would not do it like this with jQuery. Dec 26, 2023 at 9:03
  • As mentioned in my post, I want jQuery to click a link (for auto translation - my website is bilingual) after a CPT post is successfully published. I use WP Classic Editor with its plugin installed. Any advice is very appreciated.
    – daro2013
    Dec 26, 2023 at 13:05

1 Answer 1

0

I have replace the answer I gave yesterday with this newly updated and tested version. I also added some code to test with that changes Publish to Update after 6 seconds. Please give it a shot.

<input type="submit" name="save" id="publish" class="button button-primary button-large preventDefault" value="Publish">
<script>
    var publishButton = document.getElementById('publish');
    var checkPublishStatus = setInterval(function() {
        var isPublished = publishButton.value;
        console.log(isPublished);
        if (isPublished === 'Update') {
            alert('A new post has been successfully published');
            clearInterval(checkPublishStatus); // Stop the interval
        }
    }, 500); // Check every 500 milliseconds

    window.onload = function() {
        setTimeout(function(){
            document.querySelector('#publish').value = 'Update';
        }, 5000);
    }
</script>

Here is the jQuery version:


<script>
    var publishButton = $('#publish');
    var checkPublishStatus = setInterval(function() {
        var isPublished = publishButton.val();
        console.log(isPublished);
        if (isPublished === 'Update') {
            alert('A new post has been successfully published - JQUERY');
            clearInterval(checkPublishStatus); // Stop the interval
        }
    }, 500); // Check every 500 milliseconds

    $(window).on('load', function() {
        setTimeout(function(){
            $('#publish').val('Update');
        }, 5000);
    });
</script>
6
  • dear WordPress Buddha, Great work! I have tested both jQuery codes and Javascript codes, but both dont work - it seems that setInterval(function() does not work. Cheers
    – daro2013
    Dec 27, 2023 at 6:48
  • Hi daro2013, try it now, working and tested... Dec 27, 2023 at 22:35
  • WordPress Buddha Wow, it is working. I tested both jQuery and Javascript versions, they work very well. If you dont mind, may I ask another related question. What if a post is saved in Draft, then later it is published? Many thanks
    – daro2013
    Dec 28, 2023 at 3:44
  • Awesome, if you like the answer please give it an up vote by clicking the up arrow next to it. It should work as long as the button has the word "Update" as the value. You could do this: if (isPublished === 'Update' || isPublished === 'Draft') { Something liek that, depending on your situation Dec 28, 2023 at 4:12
  • 1
    Tested; not working for the Saved Draft because the value of #publish is Update, so the codes wont work for Saved Draft when that draft is published later in the future. Some modifications need to be made. Many thanks
    – daro2013
    Dec 28, 2023 at 4:43

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.