Categories: Tutorials

WordPress Heartbeat API: What It Is And How To Manage It

The WordPress Heartbeat API is a common point of discussion when it comes to speeding up a WordPress site. While it facilitates a number of key WordPress features, it can lead to significant CPU usage if left unchecked.

In this guide, we’ll take a look at what WordPress Heartbeat is, what it does, and how you can easily control its functionality to optimize your site’s performance.

What Is The WordPress Heartbeat API?

The WordPress Heartbeat API is a system that allows WordPress to send data between the server and the browser in real time. This rapid two-way communication enables features like auto-saving, revision tracking, session management, and post locking (when one user tries to edit a post that another user is currently editing).

It’s called “Heartbeat” because it simulates a pulse, with data being exchanged every 15-60 seconds.

The Heartbeat API is also available to developers, who can use it to add similar functionality to themes and plugins.

If you’re interested in the technical details, WordPress.org explains:

When the page loads, the client-side heartbeat code sets up an interval (called the “tick”) to run every 15-60 seconds. When it runs, heartbeat gathers data to send via a jQuery event, then sends this to the server and waits for a response. On the server, an admin-ajax handler takes the passed data, prepares a response, filters the response, then returns the data in JSON format. The client receives this data and fires a final jQuery event to indicate the data has been received.

In simple terms, this means the browser and the server remain in contact for the duration of a user’s session, exchanging information at regular intervals.

What The WordPress Heartbeat API Does

Let’s take a look at what the WordPress Heartbeat API actually does, and what sort of functionality it brings to the table.

Autosaving & Revisions

One of the most valuable benefits of the Heartbeat API is the ability to automatically save drafts and revisions while you’re working—without ever having to click the “Save Draft” button.

The last thing you want is to lose your hard work because you accidentally closed your browser or clicked a link to another page. In earlier versions of WordPress, this was such a common occurrence that many people were hesitant to compose content in the WordPress editor, instead opting to write in a separate application and copy the finished product into WordPress later.

Thankfully, the Heartbeat API prevents that sort of disaster by regularly sending the latest version of your post from your browser to the server.

Post Locking

Another potentially disastrous scenario is when two users try to edit the same post simultaneously. There’s no way that ends well—someone’s work is bound to get lost.

However, the Heartbeat API checks in regularly to let WordPress know that a user is currently editing a post. When someone else attempts to edit that same post, they’ll be met with this warning:

This is an important safeguard made possible by Heartbeat.

Real-Time Notifications & Comment Updating

Developers can use the Heartbeat API to make use of the “pulse” in their own themes and plugins.

One such example would be real-time notifications, delivered without requiring the user to reload the page. There are also plugins that enable the comment section to update in real time.

These are just a couple of examples. There are countless other use cases for real-time client-server communication.

How WordPress Heartbeat Influences Performance

As we’ve seen, the Heartbeat API can be incredibly useful for adding real-time functionality to WordPress. However, these features come at a cost.

At every “pulse” of the WordPress Heartbeat, a POST request is sent to /wp-admin/admin-ajax.php.

This happens every 15 seconds on post edit pages, and every 60 seconds on the main WordPress dashboard.

Yes, these requests are sent every 15-60 seconds—even if you just log into WordPress and leave your browser open.

As you can imagine, this behavior can have a notable impact on your site’s performance, especially if you have multiple users.

If your web host is showing high CPU usage attributed to /wp-admin/admin-ajax.php, the Heartbeat API is likely to blame.

How To Manage The WordPress Heartbeat API

If you find that the Heartbeat API is using excessive resources, you can speed up your website by either disabling it or managing the frequency of the pulse.

Is It Safe To Disable The Heartbeat API?

In most cases, it’s safe to disable Heartbeat if you’re the only person working on your site and you know you don’t have any important plugins or functionality that rely on it. With that said, it’s still a good idea to test this on a staging site first, if you can.

Keep in mind that if you completely disable Heartbeat, you will lose features like auto-saving, post locking, and any real-time functionality added by your plugins or theme. If you need these things, I recommend modifying the pulse frequency instead (see below).

How To Disable The Heartbeat API

Disabling the Heartbeat API can be achieved in two main ways:

  • Using a Plugin: There are plugins available specifically designed to control the Heartbeat API. Installing and activating one of these plugins allows you to easily manage and disable the Heartbeat API through a user-friendly interface.
  • Using Custom Code: Alternatively, you can implement a custom code snippet. This involves adding the aforementioned PHP code to your WordPress site. Inserting this code in your theme’s functions.php file or creating a site-specific plugin achieves the same result, effectively halting the Heartbeat API’s activity across your site.

To disable the Heartbeat API, simply add the following code to your theme’s functions.php file:

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

This will completely disable the Heartbeat API on your website. If you want to reenable it later, simply remove that code from your theme.

What Exact Code Should I Use To Disable The Heartbeat API?

To effectively disable the Heartbeat API in WordPress, you can use the following code snippet:

This code snippet should be added to your theme’s functions.php file or a site-specific plugin. It works by deregistering the ‘heartbeat’ script, effectively stopping the Heartbeat API from running.

How To Limit The Heartbeat API

If you don’t want to disable Heartbeat completely, you can limit the pulse frequency by installing the Heartbeat Control plugin.

Heartbeat Control is a highly rated free plugin from the developers of WP Rocket, which we found to be the best WordPress caching plugin.

Once you’ve installed Heartbeat Control, log into WordPress and go to Settings > Heartbeat Control Settings.

On this page, you can add various rules that dictate how the WordPress Heartbeat is allowed to behave on your website.

For each rule, you can choose to Allow Heartbeat, Disable Heartbeat, or Modify Heartbeat. The rule will apply only to the locations you select—e.g. the WordPress Dashboard, Frontend, or Post Editor.

If you choose to modify Heartbeat, a slider will appear where you can specify your desired pulse frequency in seconds. You can set it to anything from 15 to 300.

If you want to set different limits for different parts of your site, you can simply create multiple rules.

What Plugin Can You Use To Control The Heartbeat API in WordPress?

1. Once the Heartbeat Control plugin is activated, where do you go to adjust its settings?

To configure the Heartbeat Control plugin, go to your WordPress dashboard. Navigate to the ‘Settings’ menu and select ‘Heartbeat Control’. Here, you can access and adjust the plugin’s settings according to your preferences.

2. How do you start using the Heartbeat Control plugin in WordPress?

Heartbeat Control plugin, start by installing it from the WordPress plugin repository. After installation, activate the plugin via your WordPress dashboard. Once activated, you can immediately begin managing and controlling the Heartbeat API’s behavior on your site.

What Impact Did Stopping The Heartbeat API Have On A User’s Website Performance According To A Testimonial In The Passage?

According to a user’s firsthand experience shared in the passage, disabling the Heartbeat API resulted in noticeable improvements in their website’s performance. Specifically, they observed a substantial reduction in their homepage’s loading time, which decreased from four seconds to just 1.9 seconds. Additionally, there was a significant decrease in the total page size by approximately 200kb. These optimizations contributed to an improved Google PageSpeed score by two points, indicating enhanced overall website efficiency and speed.

Can disabling the Heartbeat API impact SEO results?

When evaluating changes to critical WordPress components like the Heartbeat API, it’s crucial to understand how they can affect SEO. Disabling the Heartbeat API can indeed influence SEO outcomes, with predominantly positive effects.

Key Benefits Explored:

  1. Improved Page Load Speed: Disabling the Heartbeat API has proven to notably enhance homepage loading times. For instance, users have experienced reductions from 4 seconds to approximately 1.9 seconds. Given that site speed is a pivotal SEO ranking factor, faster-loading sites are favored by search engines.
  2. Reduction in Page Size: Disabling the Heartbeat API typically leads to a decrease in total page size, often by about 200KB. Smaller page sizes contribute to quicker load times and a more seamless user experience, both of which are positive signals for SEO.
  3. Higher PageSpeed Scores: Another significant benefit is the improvement in Google PageSpeed scores. Even a modest increase in these scores can positively impact search engine rankings, as Google uses them to evaluate site performance.

Disabling the Heartbeat API can bring substantial enhancements to website performance metrics directly linked to SEO effectiveness. By boosting page speed, reducing page size, and elevating PageSpeed scores, your site becomes more competitive in search engine results. This improvement can translate into increased visibility, higher traffic volumes, and potentially greater conversion rates. It’s important to carefully weigh these benefits against any potential drawbacks and to monitor your site’s functionality closely after implementing such changes.

What Are Some Common Comments And Questions From Readers About The Heartbeat API In WordPress?

Common Questions and Comments About the Heartbeat API in WordPress

  1. Is the article about the Heartbeat API misplaced on a website named WPBeginner? WPBeginner is committed to educating users of all levels about various aspects of WordPress, including advanced features like the Heartbeat API. Despite its name suggesting a focus on beginners, the site covers a wide range of topics to ensure comprehensive learning for both novice and experienced users alike.
  2. Is WPBeginner only about beginner tutorials? While WPBeginner primarily provides tutorials tailored for WordPress beginners, the site also delves into intermediate and advanced topics. This approach ensures that users at every proficiency level can access valuable and informative content that meets their needs.
  3. I use the kk star ratings plugin; following your instructions does not remove admin-ajax.php. What should I do? If admin-ajax.php persists despite following instructions, it indicates that the kk star ratings plugin relies on WordPress’s AJAX infrastructure, which the Heartbeat API also utilizes. Contacting the plugin’s support team for tailored advice or exploring alternative plugins that do not depend on admin-ajax.php might be necessary.
  4. Does the Heartbeat API impact SEO results? The Heartbeat API itself does not directly influence SEO because it operates on backend processes. However, if excessive API activity increases server load, it could indirectly impact page load times. Slower loading times can affect SEO rankings as search engines prioritize faster sites for better user experience.
  5. Can I disable the Heartbeat API on a membership site with many simultaneous logins? Disabling the Heartbeat API on a high-traffic membership site can help alleviate server resource usage, potentially improving overall performance. Ensure to test this change in a staging environment first to avoid disrupting critical functionalities such as post autosaving and user session management essential for membership site operations.

Final Thoughts

The WordPress Heartbeat API enables a lot of great features by providing real-time communication between the browser and the server. Auto-saving and post locking, for example, solve some of the most frustrating problems from earlier versions of WordPress.

These features do come at a cost, though. The more time you spend in your WordPress dashboard, the more CPU requests will pile up, leading to higher resource usage, slower performance, and in more serious cases, hosting account suspensions.

Luckily, it’s quite simple to mitigate those issues by managing the behavior of the WordPress Heartbeat API.

If you have any questions about WordPress Heartbeat, please feel free to leave them in the comments below!

If you’re looking for fast wordpress hosting as well as done-for-you updates such as optimizing and managing your WordPress Heartbeat API effectively , check out our hosting packages by clicking the button below:

Brad Merrill

Brad Merrill is the CEO of Merrill Media and Editor in Chief of GigaPress.

Recent Posts

How to Reset WordPress: Step-by-Step Guide for Beginners

Are you looking to reset a WordPress site and start fresh? Reset a WordPress Site…

5 days ago

WordPress vs. Wix: Which Platform Is Right for You?

Ready to build your online presence? Choosing the right platform between Wix vs. WordPress is a crucial first…

5 days ago

How to Change Favicon in WordPress: A Step-by-Step Guide

Changing your favicon in WordPress is a small yet powerful way to improve your website’s…

5 days ago

How to Embed a PDF in WordPress

Have you been searching for the easiest way to share PDF files directly on your…

1 week ago

How to Add Social Media Icons to WordPress Header: A Complete Step-by-Step Guide

Ever notice how professional websites seamlessly display social media icons in their headers? Adding social…

1 week ago

How to Clear Cache on WordPress: A Step-by-Step Guide

WordPress caching is an incredible tool for boosting your website’s performance. It helps deliver faster…

1 week ago