Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use heartbeat_tick action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
November 1, 2022
5 minutes read

heartbeat_tick action

Allows the transport to be easily replaced with long-polling.

To use heartbeat_tick action, first you have to register it using add_action. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function weplugins_execute_on_heartbeat_tick_event which takes 2 parameters and we registered using add_action. The first parameter heartbeat_tick is name of the hook, The second parameter weplugins_execute_on_heartbeat_tick_event is name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_action to remove heartbeat_tick action.

Parameters

    Below are the 2 parameters required to use this hook.

  • $response: (array) The Heartbeat response.
  • $screen_id: (string) The screen ID.

Live Examples

Example 1: Basic Usage

This example demonstrates a basic implementation of the heartbeat_tick action.

    function weplugins_execute_on_heartbeat_tick_event($response, $screen_id){
       // Code to be executed when this action occurs.
    }
    // Add the action
    add_action( "heartbeat_tick", "weplugins_execute_on_heartbeat_tick_event" , 10, 2);
    

Example 2: Custom Response Handling

In this example, we modify the Heartbeat response based on the screen ID.

    function weplugins_custom_heartbeat_response($response, $screen_id){
       if($screen_id == 'dashboard'){
          $response['custom_data'] = 'This is custom data for the dashboard screen.';
       }
       return $response;
    }
    // Add the action
    add_action( "heartbeat_tick", "weplugins_custom_heartbeat_response" , 10, 2);
    

Example 3: Removing the Hook

To remove a hook callback, use the example below.

    // Remove the action
    remove_action( "heartbeat_tick", "weplugins_execute_on_heartbeat_tick_event", 10, 2 );
    

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Contact Us

If you need customization or are having trouble using this hook, please contact us. We’d be happy to assist you.

Access Premium WordPress Plugins

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.