Created
August 18, 2020 05:50
-
-
Save raazon/18ac77db67a2126bc802dd16eaad0648 to your computer and use it in GitHub Desktop.
How to enable WordPress Heartbeat API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function ($) { | |
| "use strict"; | |
| $(document).ready(function () { | |
| // Sending Data to the Server | |
| jQuery(document).on('heartbeat-send', function (event, data) { | |
| // Add additional data to Heartbeat data. | |
| data.myplugin_customfield = 'some_data'; | |
| }); | |
| // 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); | |
| }); | |
| }); | |
| })(jQuery); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php defined('ABSPATH') || die('Cheatin’ uh?'); // Cannot access pages directly. | |
| /** | |
| * REF: https://developer.wordpress.org/plugins/javascript/heartbeat-api/ | |
| * @author Razon Komar Pal | |
| */ | |
| // enqueue the Heartbeat API JS | |
| add_action('init', 'myplugin_enqueue_heartbeat_init'); | |
| function myplugin_enqueue_heartbeat_init() | |
| { | |
| // enqueue heartbeat.js | |
| wp_enqueue_script('heartbeat'); | |
| } | |
| // Add filter to receive hook, and specify we need 2 parameters. | |
| add_filter('heartbeat_received', 'myplugin_receive_heartbeat', 10, 2); | |
| /** | |
| * 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). | |
| */ | |
| function myplugin_receive_heartbeat($response, $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; | |
| } | |
| // Limit Heartbeat interval | |
| add_filter('heartbeat_settings', 'myplugin_heartbeat_interval'); | |
| function myplugin_heartbeat_interval($settings) | |
| { | |
| // ref: https://github.com/WordPress/WordPress/blob/master/wp-includes/js/heartbeat.js | |
| $settings['interval'] = 30; // Anything between 15-120 // Connect interval (in seconds). | |
| return $settings; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment