How it works
-
Add additional fields to the data to be sent (JS heartbeat-send event) -
Detect sent fields in PHP, and add additional response fields ( heartbeat_received filter) -
Process returned data in JS (JS heartbeat-tick )
Using the API
Sending Data to the Server
jQuery( document ).on( 'heartbeat-send', function ( event, data ) { // Add additional data to Heartbeat data. data.myplugin_customfield = 'some_data'; });
Receiving and Responding on the Server
/** * Receive Heartbeat data and respond. * * Processes data received via a Heartbeat request, and returns additional data to pass back to the front end. * * @param array $response Heartbeat response data to pass back to front end. * @param array $data Data received from the front end (unslashed). * * @return array */ function myplugin_receive_heartbeat( array $response, array $data ) { // If we didn't receive our data, don't send any back. if ( empty( $data['myplugin_customfield'] ) ) { return $response; } // Calculate our data and pass it back. For this example, we'll hash it. $received_data = $data['myplugin_customfield']; $response['myplugin_customfield_hashed'] = sha1( $received_data ); return $response; } add_filter( 'heartbeat_received', 'myplugin_receive_heartbeat', 10, 2 );
Processing the Response
jQuery( document ).on( 'heartbeat-tick', function ( event, data ) { // Check for our data, and use it. if ( ! data.myplugin_customfield_hashed ) { return; } alert( 'The hash is ' + data.myplugin_customfield_hashed ); });