I am trying to automatically force a browser refresh on a specific page when the page is beeing updated in the backend. I'm trying to achieve by using the Heartbeat API.
I look through all Heartbeat examples i could find but I can't grasp the whole concept of it.
All I am able to do is console.log a string every 15 sec on this specific page.
What I have so far:
<?PHP
//embed heartbeat api
function heartbeat_test_enqueue($hook_suffix) {
if ( is_page(1105)) {
// Make sure the JS part of the Heartbeat API is loaded.
wp_enqueue_script('heartbeat');
// Output the test JS in footer.
add_action( 'print_footer_scripts', 'heartbeat_test_js', 20 );
//Add filter to receive hook, and specify we need 2 parameters.
add_filter( 'heartbeat_received', 'dw_receive_heartbeat', 10, 2 );
}
}
add_action( 'wp_enqueue_scripts', 'heartbeat_test_enqueue' );
//clientside
function heartbeat_test_js() {
?>
<script>
jQuery(document).ready( function($) {
// Hook into the heartbeat-send
jQuery( document ).on( 'heartbeat-send', function( e, data ) {
//
});
// Listen for the custom event "heartbeat-tick" on $(document). This fire's once every minute that the page is open.
jQuery(document).on( 'heartbeat-tick', function(e, data) {
console.log("tick");
});
});
</script>
<?php
}
//change heartbeat interval time
function m_heartbeat_settings( $settings ) {
$settings['interval'] = 15; //only values between 15 and 60 seconds allowed
return $settings;
}
add_filter( 'heartbeat_settings', 'm_heartbeat_settings' );
//heartbeat api end
?>
I wanted to use the "post_updated" hook to check if the "post_date" has changed. But I dont get how I can use this hook in combination with the Heartbeat API.
Thanks in advance, I am really lost here.