HomeWordPressHow to Manage the WordPress Heartbeat API for Better Performance

How to Manage the WordPress Heartbeat API for Better Performance

Disclosure

The WordPress Heartbeat API facilitates real-time data synchronization between the server and users’ browsers. It powers features that enhance site management and user experience, such as dashboard notifications, post-locking, and automatic saving of drafts.

However, without proper management, it can lead to performance issues, draining server resources and slowing down your site. This guide will walk you through optimizing the Heartbeat API for improved performance, including code snippets and plugin recommendations.

Understanding the WordPress Heartbeat API

What is the Heartbeat API?

The Heartbeat API sends periodic AJAX requests (pulses) from the browser to the server, ensuring real-time communication.

  • The Heartbeat gathers the required data, sends it to the server via a jQuery event, and then awaits a response.
  • The server’s admin-ajax handler receives the data, prepares a response, filters it, and returns data in JSON format.
  • The client receives the data and fires a jQuery event to indicate the data has been received.

While beneficial, excessive requests can strain server resources, especially on high-traffic sites.

How is the WordPress Heartbeat API used?

Here are specific use cases illustrating how the WordPress Heartbeat API is employed within WordPress:

1. Autosave and Post Locking

  • Autosave: The Heartbeat API enables the autosave feature, automatically saving posts or page changes as a draft. This ensures that work is not lost if the browser crashes or the internet connection is lost.
  • Post Locking: It prevents multiple users from simultaneously editing the same post or page. When a user is editing, the Heartbeat API sends a signal to lock the post, displaying a notification to other users that the post is being edited.

2. Dashboard Notifications

The API is used to refresh WordPress dashboard widgets in real-time. For instance, it updates the number of pending comments without requiring a page reload, allowing administrators to see notifications and updates instantly.

3. User Session Management

WordPress Heartbeat helps manage user sessions by sending periodic pulses to keep them alive or log users out when their session expires. This ensures that users do not lose unsaved work if they are automatically logged out due to inactivity.

4. Real-time Data Display

For plugins and themes requiring real-time data display (like analytics, stock tickers, or social media feeds), the Heartbeat API can fetch the latest data at regular intervals, updating the displayed information without the need for a manual page refresh.

Identifying Performance Issues

Performance issues related to the WordPress Heartbeat manifest as:

  • Slow dashboard response times,
  • High CPU usage, and
  • Potential conflicts with AJAX-dependent plugins or themes.

Tools like GTmetrix or Query Monitor can help diagnose these issues by monitoring AJAX calls and server load.

Managing the Heartbeat API

So, how does one handle issues caused by the Heartbeat API?

Well, WordPress provides ways to manage the Heartbeat API’s behavior. You can control its frequency or disable it in specific areas of your site where real-time communication isn’t critical. This can be achieved through plugins like Heartbeat Control by WP Rocket or custom code snippets added to your theme’s functions.php file.

Adjusting Heartbeat Frequency via Code

To reduce the frequency of Heartbeat API requests, you can modify the pulse interval with a function in your theme’s functions.php file or a site-specific plugin:

add_filter( 'heartbeat_settings', 'adjust_heartbeat_frequency' );
function adjust_heartbeat_frequency( $settings ) 
{
    $settings['interval'] = 60; // Adjust the interval to 60 seconds
    return $settings;
}

Make sure you add this code snippet in your child theme. If you don’t have one, create or install a child theme to avoid your changes being overwritten by a theme update.

This snippet changes the Heartbeat API’s pulse interval to 60 seconds, reducing the load on your server.

Disable WordPress Heartbeat via Code

To disable the Heartbeat API altogether, you can add the following code snippet to your theme’s functions.php file or a site-specific plugin:

add_action( 'init', 'stop_heartbeat', 1 );
function stop_heartbeat() {
	wp_deregister_script('heartbeat');
}

This code uses the wp_deregister_script function to deregister the Heartbeat script, effectively disabling the API. Remember that this will disable Heartbeat API functionality across your entire site, including the WordPress admin dashboard.

As mentioned earlier, deploy this code in your child theme.

In general, I would not recommend disabling the heartbeat API, unless you have a very specific scenario that demands it, or for some testing. Your best option is to reduce the frequency of the heartbeat rather than disabling it altogether.

Using Plugins for Heartbeat Control

For those who prefer not to use code, plugins like “Heartbeat Control by WP Rocket” offer a user-friendly interface for managing the Heartbeat API.

Installing and Configuring Heartbeat Control:

1. Installation:

  • Go to your WordPress dashboard, navigate to Plugins, and select the Add New option.
  • Search for “Heartbeat Control by WP Rocket”
  • Click “Install Now” and then activate the plugin.
Heartbeat Control by WP Rocket
Heartbeat Control by WP Rocket

2. Enable or Disable Heartbeat:

  • Once activated, navigate to “Settings” > “Heartbeat Control”
  • Here, you can adjust the frequency of the Heartbeat API across the dashboard, front-end, and post editor. You can turn off the heartbeat for some or all of the above using the disable heartbeat option.
Heartbeat Control - Enable Disable option
Heartbeat Control – Enable Disable option

3. Modify Heartbeat

  • Set the control behavior to Modify Heartbeat and choose a slower rate or disable it in areas where real-time updates are not required.
Heartbeat Control-Modify Frequency
Heartbeat Control-Modify Frequency

Monitoring and Maintenance

After implementing optimizations, monitor your site’s performance with tools like Query Monitor. Regularly check for updates to any plugins used for Heartbeat control and review your custom code for compatibility with the latest WordPress versions.

If performance issues persist or new issues arise after optimizing the Heartbeat API, review your changes for syntax errors or conflicts with other plugins. Reverting recent changes or systematically deactivating plugins can help identify the cause.

Conclusion

Optimizing the WordPress Heartbeat API is essential for maintaining a fast and responsive WordPress website. By adjusting the API’s frequency, disabling it in non-essential areas, and using plugins for easy management, you can ensure that your site benefits from real-time features without compromising performance. Keep your customizations up-to-date and monitor your site’s performance to adapt to changing needs and traffic patterns.

You may also like:

How to Install WordPress Plugins in Three Ways

How to Enable Object Caching in WordPress

The Art of Minimalist Website Design

 

Disclosure: Coolfundas is reader-supported. When you buy through links on our site, we may earn an affiliate commission at no extra cost to you. Read more about the disclosure.

↑ Back to Top

Shashi Venkatesh
Shashi Venkatesh
Shashi Venkatesh is a 22-year veteran of the technology industry, with experience developing and managing large-scale web applications for clients. He is a WordPress expert and has consulted extensively in the development of WordPress websites, blogs and ecommerce platforms.

LEAVE A REPLY

Please enter your comment!
Please enter your name here