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

How to use heartbeat_nopriv_tick action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
April 8, 2023
5 minutes read

In the world of WordPress, hooks are magical tools that allow developers to extend and customize functionality without touching the core code. One such powerful hook is the heartbeat_nopriv_tick action. This action lets you replace the transport with long-polling, giving you more control over how tasks are executed in the background.

To get started with the heartbeat_nopriv_tick action, you’ll need to register it using add_action. You can add this code to your theme’s functions.php file or create a custom WordPress plugin, which is recommended to ensure nothing breaks when your theme updates.

Let’s dive into some live examples to see how you can use this hook effectively:

Example 1: Registering the Hook

In this example, we define a function weplugins_execute_on_heartbeat_nopriv_tick_event that takes two parameters. We register it using add_action, specifying the hook name, function name, priority, and number of arguments.

    function weplugins_execute_on_heartbeat_nopriv_tick_event($response, $screen_id){
       // Custom code to be executed when the action occurs
    }
    // add the action
    add_action("heartbeat_nopriv_tick", "weplugins_execute_on_heartbeat_nopriv_tick_event", 10, 2);
    

Example 2: Removing the Hook

If you ever need to remove a registered hook, you can use remove_action. Ensure you provide the same callback function name, priority, and number of arguments as when registering.

    remove_action("heartbeat_nopriv_tick", "weplugins_execute_on_heartbeat_nopriv_tick_event", 10, 2);
    

Example 3: Utilizing the Hook with Custom Logic

Here’s an example of how you might implement custom functionality using the parameters received by the function.

    function weplugins_custom_logic_on_heartbeat($response, $screen_id){
       // Implement additional functionality according to your website requirements
    }
    // add the action
    add_action("heartbeat_nopriv_tick", "weplugins_custom_logic_on_heartbeat", 10, 2);
    

Access Premium WordPress Plugins

Contact Us

If you need any customization or run into trouble using this hook, feel free to contact us. We’re here to help you make the most out of your WordPress site!

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.