Cloud Service >> Knowledgebase >> WordPress >> How to Set Frequency of WordPress Heartbeat and Limit Executions
submit query

Cut Hosting Costs! Submit Query Today!

How to Set Frequency of WordPress Heartbeat and Limit Executions

The WordPress Heartbeat API is a powerful tool that enables real-time communication between your server and the WordPress dashboard. While it offers great features like auto saving posts and live notifications, its frequent execution can put a strain on your hosting resources, especially if you're running a shared or colocation setup. This guide explains how to manage the Heartbeat API effectively by setting its frequency and limiting its executions to optimize your website’s performance.

What is the WordPress Heartbeat API?

The Heartbeat API uses admin-ajax.php to send requests between the browser and the server at regular intervals. These periodic pings enable:

Post autosaving to prevent data loss.

Session management to log out idle users.

Real-time updates like live plugin notifications.

However, the frequent requests can lead to:

Increased server load.

Performance degradation, especially on limited hosting environments.

Why Optimize the Heartbeat API?

Optimizing the API is essential for websites with high traffic or limited server resources:

Reduced Server Load: Lower the number of requests made to the server.

Improved Performance: Ensure smooth operations on cloud hosting setups with resource constraints.

Cost-Efficiency: Optimize server usage in colocation or shared hosting environments.

How to Set Heartbeat Frequency

You can control the frequency of Heartbeat API pings to balance functionality and performance.

1. Use a Plugin

Plugins simplify the process of adjusting the Heartbeat API.

Install a performance optimization plugin.

Navigate to the Heartbeat settings and specify the frequency (in seconds).

Common frequencies include:

15 seconds for intensive tasks.

60 seconds for standard usage.

2. Manual Configuration

For more control, manually edit your theme’s functions.php file:

add_filter('heartbeat_settings', 'custom_heartbeat_frequency');  

function custom_heartbeat_frequency($settings) {  

    $settings['interval'] = 60; // Set to 60 seconds  

    return $settings;  

}  

This approach works best for those familiar with coding and server management.

How to Limit Heartbeat Executions

1. Disable Heartbeat in Unused Areas

If you don’t need real-time updates in certain parts of your site, you can disable the API:

  • Use a plugin or add code to functions.php:

add_action('init', 'stop_heartbeat', 1);  

function stop_heartbeat() {  

    wp_deregister_script('heartbeat');  

}  

Recommended Areas for Disabling:

Frontend of the website.

Post editor when autosave isn’t needed.

2. Limit Heartbeat to Specific Pages

You can restrict the API to specific admin pages:

add_action('init', 'restrict_heartbeat');  

function restrict_heartbeat() {  

    if (is_admin() && !is_page('post-edit.php')) {  

        wp_deregister_script('heartbeat');  

    }  

}  

This approach ensures the API is active only where necessary, reducing unnecessary server load.

Testing and Monitoring

After implementing changes, it’s important to test their impact:

Monitor Server Performance:

Use hosting control panels to check resource usage.

Tools like New Relic or built-in server analytics can help.

Test Site Functionality:

Ensure autosave and real-time updates still work where required.

Check other plugins for compatibility issues.

Adjust Settings as Needed:

If server load remains high, further reduce the frequency.

Re-enable the API in areas where functionality is compromised.

Advanced Tips for Optimized Hosting Environments

Upgrade Hosting Resources:

High-frequency Heartbeat pings are better suited for robust servers or colocation setups.

Consider upgrading to hosting plans with more CPU and memory capacity.

Leverage Content Delivery Networks (CDNs):

Offload resource-heavy tasks like real-time updates to CDNs.

This reduces the workload on your server, especially in shared hosting environments.

Integrate Load Balancers:

Spread traffic evenly across multiple servers to prevent overload.

Particularly useful for high-traffic WordPress sites using colocation hosting.

Conclusion

 

The WordPress Heartbeat API is a valuable feature, but its improper configuration can lead to performance issues. By setting a reasonable frequency and limiting executions to necessary areas, you can optimize server performance and reduce unnecessary load. These steps are especially crucial for shared hosting or colocation environments where resources are finite. With the right balance, your WordPress site can enjoy the benefits of the Heartbeat API without compromising on speed or reliability.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!