access_time
hourglass_empty
person_outline

What is WordPress Heartbeat API and How to Manage it

In 2013 WordPress introduces Heartbeat API – a feature that allows your browser to automatically communicate with the server. However, there are a couple of drawbacks of this feature.

If you use a shared hosting plan, you need to take a closer look at the CPU usage of your server. That’s because some hosting companies might suspend your account when you go over the limit.

Here, we’ll talk why WordPress Heartbeat API is it the usual suspect that causes the issue.

What is the WordPress Heartbeat API?

WordPress Heartbeat API provides a communication protocol, using AJAX calls, between browser and server.

As the name implies, the API will send continuous pulses and triggers events (or callbacks) upon receiving data. This function helps you to sync all the data between the server and the WordPress dashboard.

The idea behind heartbeat API is quite compelling. For example, when you create/edit post from the editor, it provides a function to autosave the post periodically.

In a collaborative WordPress site, it has a post-locking function, to prevent you from editing a post that’s being worked on by another user. If you use an ecommerce plugin, this API will also help to display for sales that happen on your site.

Why Limit WordPress Heartbeat API?

While it’s beneficial to have an autosave function and real-time data notification features, it can be harmful in a specific situation.

Heartbeat API sends AJAX requests (POST requests) using the /wp-admin/admin-ajax.php file. Each request that executes PHP file is equal to CPU time on the server.

This can cause a large number of requests sent to the hosting server, which results in high CPU utilization.

As we noted earlier, this can be a problem for a webmaster with a shared hosting plan. As you reach your quota limit, high CPU usage can eventually lead to account suspension.

So if you’re having an issue that causes spikes in POST-admin-ajax.php, here is a tutorial that might help you to fix that.

Things to Consider Before Stopping Heartbeat API

If you’re thinking to stop the Heartbeat API completely, you need to think about it twice. It’s not because you can’t do that, it’s just not practical.

Without the WordPress Heartbeat API, all the changes you made in the post will be lost should you ever forget to click the Save Draft button. And you will no longer have access to Revisions feature in WordPress.

If you install plugins that use Heartbeat API, you also can’t display the real-time notifications and information features on your site.

But, if you’re working alone, and all of these functions do not matter much to you, you can then proceed to stop the WordPress Heartbeat API.

Otherwise, you might consider controlling the API instead.

How to Limit the WordPress Heartbeat API With a Plugin?

You can control how often the Heartbeat API to send callbacks to your server using the Heartbeat Control plugin. If you’re new in WordPress, please take a look at our tutorial about how to install WordPress plugins.

Once you installed and activated the plugin, go to Settings -> Heartbeat Control Settings. Here, you can set a certain rule for the API.

  • Heartbeat Behavior – allows you to Enable Heartbeat, Disable Heartbeat, or Modify Heartbeat
  • Locations – determines which area you want to control the API from, it provides you with three options: WordPress Dashboard, Frontend, and Post Editor
  • Frequency – the number of callbacks you allow for each determined location. The value ranges from 15 to 300 seconds

WordPress Heartbeat Rule In the example, we limit the frequency of Heartbeat API function in Post Editor to 200 seconds. This is a more practical approach if you have multiple authors working on the same WordPress site.

You can also set the value differently in each location you wish to control. Just click Add Another Value button, and adjust it accordingly.

This plugin also allows you to completely disable the WordPress Heartbeat API altogether. You can do so by choosing Disable Heartbeat for the Heartbeat Behavior.

How to Completely Stop Heartbeat API Without a Plugin?

You can also stop the Heartbeat API without a plugin, by adding this code snippet to your active theme functions.php file.

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

Please remember, you should do this if you’re the only person working with the website and you don’t need to install additional plugins that use this API.

From your WordPress Dashboard, go to Appearance -> Theme Editor. Make sure you’re editing the active theme of your site. Click Theme Functions (functions.php) from the right-hand navigation menu. Paste the code snippet above right after the opening <?php tag and click Update.Edit the function.php file to disable WordPress heartbeat

That’s it, and now you’ve successfully disabled the WordPress Heartbeat API on your site.

Conclusion

You’ve learned about the WordPress Heartbeat API and the functionality it gives to your site. Although it’s undoubtedly beneficial, in a shared hosting environment it can cause quite a problem should you not handling it well.

To reduce the CPU usage on your server, you can either control the API or completely disable it. Also, you can do it using a plugin or manually adding code to the functions.php file.

The Author

Author

Luqmanul M. / @luqman

Luqman is a self-proclaimed social scientist. He is passionate about education, technology, and everything in between. He wants to help create a high-quality education system. Having spent the past four years as a social researcher and blog guru, he lends his skills to Hostinger's digital content team. As for free time, he enjoys reading scientific (and not-so-scientific) literature with a cup of black arabica coffee as a companion.

Related tutorials

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in WordPress
How to Block IP Addresses Using IP Manager and .htaccess
Close