WordPress Refresh on Update

<script> function poll(poll_url, poll_timeout) {     setInterval( function() {         $.ajax({         dataType: "json",         url: poll_url,         success: function (data){         if(data.modified != date_modified){         if(!date_modified){         date_modified = data.modified;         }         else{         location.reload();         }         }                 }         });     }, poll_timeout); } var date_modified = null; poll("<?php echo site_url() ?>/wp-json/wp/v2/<?php echo $post->post_type ?>s/<?php echo $post->ID ?>", 3000); </script>
Initially this was to be used with the Rest API plugin but Rest is native now with WP so you don't need the plugin anymore.

3 Responses

How does it work?
@God is good The code above uses the REST API plugin as sort of a heart beat. So you would add the code to your index.php, or page.php for example. When you publish or when you update the page the clients computer will automatically refresh.
@God is good Sorry I'm wrong about that you don't need the first part, that's for something else. So I deleted that. You only need the one script currently above. It polls the site every three seconds. When it sees a change it will automatically refresh the client machine with the new changes.

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.