BACK TO TOP

What is WordPress Heartbeat API and How to Manage it

What is WordPress Heartbeat API and How to Manage it

The WordPress Heartbeat API is a feature introduced to WordPress in version 3.6 and has since provided near real-time communication between your server and your browser while you are logged in to your WordPress admin dashboard. This feature has helped developers connect to the web server quickly, which is why it has been used for numerous functionalities.

It works by sending ticks, i.e. “pulse” between the server and the browser in regular time intervals, most commonly 15 seconds. And, while it is undeniably useful, it can lead to high CPU usage on the server, especially if multiple admins are editing or running other tasks on your website. Of course, this can lead to website performance issues, especially if you are using shared hosting.

With that in mind, we have decided to compile this article, explaining more closely how you can manage the WordPress Heartbeat API. Specifically, we will be discussing what it is exactly and what to consider before limiting or completely disabling it, as well as how you can do it.

Here’s what we’ll be talking about in this article:

Qode Themes: Top Picks
Bridge New Banner
Bridge

Creative Multi-Purpose WordPress Theme

Stockholm WordPress Theme
Stockholm

A Genuinely Multi-Concept Theme

Startit WordPress Theme
Startit

Fresh Startup Business Theme

Understanding the WordPress Heartbeat API

The WordPress Heartbeat API provides communication between the browser and the web server using AJAX calls. More precisely, it relies on the /wp-admin/admin-ajax.php file to send POST requests to the server and trigger callback events with the received website data. By default, these requests are sent periodically, when each logged-in user is using the admin dashboard, on the website’s frontend, or while editing a post or page.

Examples where the WordPress Heartbeat API is used are many and include creating autosaves and managing revisions in the post editor, managing notifications using the WordPress admin dashboard, post-locking when multiple users are editing the same post, showing when a user has been timed out for being idle, as well as managing real-time sales data for e-commerce plugins. However, despite the usefulness of these features, numerous AJAX requests can lead to high CPU usage or even server overload on some occasions. As a result, your website performance will be slower, your pages will be indexed at a slower rate, and in extreme cases, it can lead to your hosting account being suspended due to excessive use of server resources.

To avoid such problems, you should consider managing the WordPress Heartbeat API in some form or capacity. You can either choose to limit the frequency of the “ticks” or to completely disable the Heartbeat API. We will discuss both methods below, as well as the things you should consider before opting for either.

On Managing the WordPress Heartbeat API

In the following sections, we will cover how you can limit the frequency of the Heartbeat API POST requests, as well as how to disable the API altogether. Both can be done using a suitable WordPress plugin, as well as through custom code. Of course, we will cover both methods. But, before we proceed to the how-tos of the article, there are several things we must mention.

Firstly, a way of managing the Heartbeat API can be found in many WordPress performance-optimizing plugins, both free and premium. For this article, we have opted to showcase a free plugin called Heartbeat Control, which is one of the most well-known and built specifically for this purpose.

That said, we still strongly suggest making a backup of your website before proceeding, as limiting or, especially, disabling the Heartbeat API can negatively impact the functionalities of your plugins and theme, and a website as a whole.

Finally, if you opt to use the code snippets we included to disable or limit the Heartbeat API, then brushing up on your knowledge of FTP is also suggested. Then, having done all the necessary precautions, proceed below.

How to Limit the WordPress Heartbeat API

Generally speaking, limiting the number of requests generated by the Heartbeat API should be the go-to solution for most WordPress users, as it will decrease the CPU usage while preserving the functionalities of plugins, themes, and WordPress Core that rely on the API. Therefore, we will showcase it first.

Additionally, after performing either of the methods described below, we also advise testing whether the time interval settings were properly applied by monitoring the HTTP requests made to the /wp-admin/admin-ajax.php file. We have covered it in our section on monitoring Heartbeat POST requests later in the article, which we advise reviewing after modifying the Heartbeat API time interval. Having said that, let us proceed.

  • Using a WordPress plugin

After installing and activating the plugin, navigate to the Settings > Heartbeat Control section, which will place you in the General settings tab, which holds all the options needed for limiting or disabling the Heartbeat API. In the tab, you will see three subsections of options concerning the WordPress dashboard, the front end of the website, and the post editor, all of which use the Heartbeat API, as previously mentioned.

Each of these subsections offers the option to allow or disable the Heartbeat API, as well as modify its recurring time interval using the “Modify Heartbeat” option. By default, the “Allow Heartbeat” option is selected, while the Heartbeat API frequency, i.e. the time interval in which POST requests are called, isn’t shown.

Heartbeat Control Settings

For example, we will modify the Heartbeat API frequency within the WordPress dashboard below. To do that, we have to click the radio button next to the “Modify Heartbeat” option. After doing so, a slider will be shown, using which we can set your Heartbeat frequency from 15 to 300 seconds. It will also show the current Heartbeat frequency, whose default value is 15.

Modify Heartbeat

Then, to change the time interval in which Heartbeat “ticks” are issued, choose a value between 15 and 300 using this slider, and click the “Save Changes” button to save the changes made.

Heartbeat Frequency

For this example, we have opted to use 60 seconds as a time interval, but you can use any other within the 15 – 300 seconds range. Of course, in doing so, you should be aware of the things we mentioned above regarding proper website functionality.

After setting the time interval, a notification stating “Your changes have been saved successfully” will be shown.

Changes Saved Successfully

Then, following these steps, you can set the remaining time intervals for the Heartbeat API on the front end and in the post editor, respectively. We have opted to set the 60s interval once more, as seen below.

Fronted Post Editor

Finally, after setting the Heartbeat API frequency for all three locations, you should check the functionality of your theme and plugins in the respective WordPress areas after making changes.

  • Using custom code

To set the Heartbeat API frequency using code, you only need a simple code snippet given below. It represents a function called qode_set_heartbeat_time_interval() which is hooked onto the heartbeat_settings filter hook. The content of our custom function is simple – we have specified that the Heartbeat API interval is set to 60 seconds, stored it in the $settings variable, and later returned the variable. Of course, you can change the 60 (seconds) to any value within the 15 – 300 range, as was done using the Heartbeat Control plugin above.

function qode_set_heartbeat_time_interval( $settings ) {
$settings['interval'] = 60;
return $settings;
}
add_filter( 'heartbeat_settings', 'qode_set_heartbeat_time_interval' );

Apart from the above, there are two additional things we must mention. Firstly, since we haven’t used any conditional statements to target specific locations, this code snippet will set a uniform Heartbeat API time interval for all three locations – the WordPress dashboard, website frontend, and the post editor. And, secondly, for this code snippet to work properly, it must be added either in the functions.php file of your child theme, via FTP, or inside a site-specific plugin. We have opted for the latter and used the Code Snippets plugin we examined in the article on site-specific plugins.

We have navigated to Snippets > Add New, added the code snippet title, inserted the code into the Code section, and clicked the “Save Changes and Activate” button. As for the remaining options, we have left them at their default values, including not adding a description or tags, which can be done later, optionally.

Add New Snippet Code
Save Changes and Activate

Of course, shortly after performing the previously described actions, we got a notification stating that our code snippet was added and activated.

Snippet Added and Activated

Needless to say, what remains now is to check the functionality of your theme and plugins in the WordPress dashboard, the website’s frontend and within the post editor, as previously mentioned.

How to Disable the WordPress Heartbeat API

If limiting the WordPress Heartbeat API hasn’t reduced the server CPU usage significantly, you should consider disabling it, which will be discussed in this section. Similar to the previous one, we will show how you can disable the WordPress Heartbeat API using the Heartbeat Control plugin, as well as with a custom code snippet. However, as this could hurt some website functionalities, it is important to thoroughly think through it before deciding to disable the Heartbeat API. As mentioned before, you could lose the ability to auto-save drafts, manage revisions or the post locking feature.

Finally, disabling the Heartbeat API can hurt the functionality of any theme or plugin that relies on it. The most notable plugins that use the API are the page builders like Elementor, for which we don’t advise completely disabling the Heartbeat API. That said, let us explain how you can disable the API, should you decide to do it.

  • Using a WordPress plugin

As mentioned, the Heartbeat Control plugin can also be used to disable the Heartbeat API in the WordPress dashboard, on the frontend, and in the post editor. Of course, to do so, you need to navigate to the Settings > Heartbeat Control section, which will position you within the plugin’s General settings tab. Then, if you want to disable the Heartbeat API in the WordPress dashboard, tick the radio button next to the “Disable Heartbeat” option in the WordPress Dashboard subsection and click the “Save Changes” button.

Disable Heartbeat

You will see a success message shortly after.

Changes Saved Successfully

Similarly, you can disable the Heartbeat API for the other two locations – the website’s front end and the post editor screen. Of course, to do so, tick the radio button next to the corresponding “Disable Heartbeat” option and click the “Save Changes” button, afterward.

Disable Heartbeat

After disabling the API, it is important to check all the functionalities of your theme and plugins, as many could be affected by this change.

  • Using custom code

If you opt to disable the Heartbeat API using code, you will need an equally simple code snippet as with modifying its default time interval. For this article, we have created the following snippet that contains a custom function called qode_disable_the_heartbeat_api(), which has been “hooked” onto the init action hook. Within our custom function, we have only called the wp_deregister_script() function to remove the heartbeat script.

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

Furthermore, the same two notices we mentioned in the previous section still hold. Meaning this code will remove the Heartbeat API from all three locations. For it to work properly, it needs to be added either in the functions.php file of your child theme, via FTP, or into a site-specific plugin. Once again, we opted for the Code Snippets route.

To disable the Heartbeat API on your website using code, navigate to the Snippets > Add New. Then, insert the code snippet title, and its code in the Code section, and click the “Save Changes and Activate” button to proceed.

Save Changes and Activate

You will see the following success message shortly after.

Snippet Added and Activated

However, to ensure all is working properly on your website, we strongly advise checking the functionalities of your theme and plugins after disabling this API.

How to Monitor Heartbeat POST Requests

A simple way of monitoring Heartbeat POST requests to the /wp-admin/admin-ajax.php file can be done using the developer tools of your current browser. This can be done from any of the three previously mentioned locations – WordPress admin dashboard, website’s frontend, or the post editor screen. Of course, the purpose of the monitoring process described below is to verify whether the chosen time interval had been successfully applied. We will use the admin dashboard as an example, but the same steps can be applied regardless of the location. Let us begin.

First, right-click anywhere on the page while viewing the WordPress admin dashboard. A new menu will appear, and you should click on the “Inspect” option to access the browser’s developer tools.

Inspect

You will be taken to the Elements tab, right on the HTML element you right-clicked on. Then, you should switch to the Network tab from where you can monitor all the HTTP requests your page gets while developer tools are open.

Network

Then, you can use the Filter function to filter only the HTTP requests that request the /wp-admin/admin-ajax.php file. To do that, you can insert either /wp-admin/admin-ajax.php or just admin-ajax.php in the Filter text field, while making sure either All or Fetch/XHR HTTP requests are selected.

Network Admin Ajax All

Then, you will have to wait a while until new Heartbeat POST requests have been issued, which will be logged below. After at least one has been logged, you can click on it to access its data. By default, the Headers tab will be opened.

Network Headers

You can then switch to the Payload tab, from where you can inspect the interval value, as long as it has the heartbeat action.

Network Payload

Of course, as you wait, more requests will be created, assuming that the Heartbeat API hasn’t been disabled. Which is the reason why there are only two requests shown on the first screenshot, while there are five of them on the latter one.

This method can also be used to verify that the Heartbeat API has been successfully disabled. Specifically, if after waiting for some time (more than the set time interval), you don’t see any POST requests to the /wp-admin/admin-ajax.php file being logged in the access log, you can assume that it has been successfully disabled for the given location.

In case you find out your Heartbeat settings aren’t being applied (either the interval being wrong or the requests being generated despite the API being disabled), you should employ some basic troubleshooting to establish which plugin or theme is to blame. With that, we conclude the article.

Final Thoughts

The Heartbeat API is a feature introduced in the WordPress 3.6 version providing a way of communication between the web server and the browser. While it has some uses which we outlined, it can create an excessive amount of POST requests that can increase the server’s CPU usage significantly. Therefore, some form of management of this API is needed, either limiting the frequency in which these requests are sent or disabling it completely if it doesn’t harm the functionality of your WordPress website.

In this article, we have discussed the two ways of achieving both – using a suitable WordPress plugin or custom code. Since we mentioned all the necessary precautions and thoroughly explained all the steps, you should have no issue limiting or disabling the API using either method. And, since managing the Heartbeat API is a common performance optimization measure for WordPress websites, we strongly suggest bookmarking this article for the next time you might need to use it.

Post your comment

Comments0