The heartbeat_nopriv_send WordPress PHP Filter allows you to modify the Heartbeat API’s AJAX response when no data is passed and the user does not have privileges.
Usage
add_filter('heartbeat_nopriv_send', 'your_custom_function', 10, 2); function your_custom_function($response, $screen_id) { // your custom code here return $response; }
Parameters
$response
(array) – The no-privilege Heartbeat response.$screen_id
(string) – The screen ID.
More information
See WordPress Developer Resources: heartbeat_nopriv_send
Examples
Modify the response for a specific screen ID
Modify the Heartbeat response for a specific screen ID in no-privilege environments.
add_filter('heartbeat_nopriv_send', 'modify_response_for_screen_id', 10, 2); function modify_response_for_screen_id($response, $screen_id) { if ($screen_id === 'your_screen_id') { $response['custom_key'] = 'custom_value'; } return $response; }
Add custom data to the Heartbeat response
Add custom data to the Heartbeat response in no-privilege environments.
add_filter('heartbeat_nopriv_send', 'add_custom_data_to_response', 10, 2); function add_custom_data_to_response($response, $screen_id) { $response['your_data_key'] = 'your_data_value'; return $response; }
Remove a specific key from the Heartbeat response
Remove a specific key from the Heartbeat response in no-privilege environments.
add_filter('heartbeat_nopriv_send', 'remove_key_from_response', 10, 2); function remove_key_from_response($response, $screen_id) { unset($response['key_to_remove']); return $response; }
Change the interval of the Heartbeat API
Change the interval of the Heartbeat API in no-privilege environments based on the screen ID.
add_filter('heartbeat_nopriv_send', 'change_heartbeat_interval', 10, 2); function change_heartbeat_interval($response, $screen_id) { if ($screen_id === 'your_screen_id') { $response['heartbeat_interval'] = 45; // Set the interval to 45 seconds. } return $response; }
Log Heartbeat responses
Log the Heartbeat responses in no-privilege environments for debugging purposes.
add_filter('heartbeat_nopriv_send', 'log_heartbeat_responses', 10, 2); function log_heartbeat_responses($response, $screen_id) { error_log('Heartbeat response for screen ID ' . $screen_id . ': ' . json_encode($response)); return $response; }