Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin tommcfarlin/heartbeat.js Secret
Last active Aug 29, 2015

Embed
What would you like to do?
[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
You can’t perform that action at this time.