Home » Web Development » WordPress Pulse? – Heartbeat API

With WordPress 3.6, there will be a new API – Heartbeat. On Ticket 23216 in Trac all discussions and information be gathered together. Because Heartbeat may also have influences for users, here are some words and clues. Heartbeat is introduced to various activities, such as Autosave, locks of articles and to handle logon and logoff notifications. In parallel, the API can also be used for own developments.

On the Pulse

The default heartbeat is beating with a pulse of 15 seconds – the work the processor must do and so it may be useful to change this value. It can not be changed arbitrarily. The pulse of heartbeat should be between 5 and 60 seconds. Following small Plugin changes to the maximum value of 60s.

 <?php /**  * Plugin Name: Set Heartbeat pulse  */ 	 ! defined( 'ABSPATH' ) and exit; 	 add_filter( 'heartbeat_settings', 'fb_heartbeat_settings' ); function fb_heartbeat_settings( $  settings = array() ) {  	$  settings['interval'] = 60; 	 	return $  settings; }  

In parallel, you can also stop Heartbeat, disconnect the pulse of what the following small Plugin does. But, this is a hard way to deactivate the API and not easy to use. If you will stop the API, please test after deactivation.

 <?php /**  * Plugin Name: Remove Heartbeat pulse  */ 	 ! defined( 'ABSPATH' ) and exit;  remove_action( 'admin_init', 'wp_auth_check_load' ); 	 

Debugging

The API was implemented in the core of WordPress, so that the console can give infos. Therefore the parameter wp.heartbeat.debug has to set on TRUE and the JavaScript can give informations.

 if ( typeof console !== 'undefined' ) { 	// Show debug info 	wp.heartbeat.debug = true; } 

Development

A simple example can find in Trac, where the Dashboard Widget for comments gets refreshed by the pulse – so you can see a refresh of the numbers every 15s, as new comments come in. The example shows the most important hooks and functions and there doesn’t need more explanation.

Further information, links to this topic


© WP Engineer Team, All rights reserved (Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)

WP EngineerWP Engineer

Leave a Reply