Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active August 29, 2015 13:57
Show Gist options
  • Save tommcfarlin/f1d8785e16d8e4780d92 to your computer and use it in GitHub Desktop.
Save tommcfarlin/f1d8785e16d8e4780d92 to your computer and use it in GitHub Desktop.
[WordPress] A practical example of the Heartbeat API.
(function( $ ) {
"use strict";
$(function() {
$( document ).on( 'heartbeat-send', function( e, data ) {
data['client'] = 'acme_request_embed_type';
});
$( document ).on( 'heartbeat-tick', function( e, data ) {
switch ( data['server'] ) {
case 'hide_video':
if( false === $('#bumpers').is(':visible') ) {
location.reload();
} // end if
break;
case 'display_video':
if( false === $('#video').is(':visible') ) {
location.reload();
} // end if
break;
} // end switch/case
});
});
}( jQuery ));
<?php
function acme_heartbeat_receive( $response, $data ) {
if ( 'acme_request_embed_type' == $data['client'] ) {
switch ( get_theme_mod( 'acme_video_embed_display' ) ) {
case 'video':
$response['server'] = 'display_video';
break;
case 'bumpers':
default:
$response['server'] = 'hide_video';
break;
} // end switch/case
} // end if
return $response;
} // end live_heartbeat_receive
add_filter( 'heartbeat_received', 'acme_heartbeat_receive', 10, 2 );
add_filter( 'heartbeat_nopriv_received', 'acme_heartbeat_receive', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment