The heartbeat_nopriv_received WordPress PHP Filter allows you to modify the Heartbeat API’s Ajax response in no-privilege environments, where users don’t have access to specific actions or features.
Usage
add_filter('heartbeat_nopriv_received', 'your_custom_function', 10, 3); function your_custom_function($response, $data, $screen_id) { // your custom code here return $response; }
Parameters
$response
(array) – The no-priv Heartbeat response.$data
(array) – The$_POST
data sent.$screen_id
(string) – The screen ID.
More information
See WordPress Developer Resources: heartbeat_nopriv_received
Examples
Add a custom message to the Heartbeat response
Append a custom message to the Heartbeat response for users without privileges.
add_filter('heartbeat_nopriv_received', 'append_custom_message', 10, 3); function append_custom_message($response, $data, $screen_id) { $response['custom_message'] = 'You are not logged in!'; return $response; }
Log no-priv Heartbeat data
Log the no-priv Heartbeat data to a file for debugging purposes.
add_filter('heartbeat_nopriv_received', 'log_heartbeat_data', 10, 3); function log_heartbeat_data($response, $data, $screen_id) { error_log(print_r($data, true), 3, '/path/to/your/debug.log'); return $response; }
Check for specific screen ID
Perform an action only if the Heartbeat is sent from a specific screen ID.
add_filter('heartbeat_nopriv_received', 'check_screen_id', 10, 3); function check_screen_id($response, $data, $screen_id) { if ($screen_id == 'your_specific_screen_id') { // your custom code here } return $response; }
Modify response based on data
Change the Heartbeat response depending on the data sent.
add_filter('heartbeat_nopriv_received', 'modify_response_based_on_data', 10, 3); function modify_response_based_on_data($response, $data, $screen_id) { if (isset($data['custom_key']) && $data['custom_key'] == 'example_value') { $response['custom_response'] = 'Data received'; } return $response; }
Add timestamp to response
Add the current timestamp to the Heartbeat response.
add_filter('heartbeat_nopriv_received', 'add_timestamp_to_response', 10, 3); function add_timestamp_to_response($response, $data, $screen_id) { $response['timestamp'] = time(); return $response; }