How to manage the WordPress Heartbeat API

The WordPress Heartbeat API has been around for nearly a decade. It provides seamless real-time communication between the WordPress dashboard and the host server. The Heartbeat API is built into the core of WordPress and provides powerful features that can significantly improve the website management experience.

However, this power comes at a cost. It is the amount of server resources needed to keep communications running smoothly. Running resource-intensive websites or having your hosting service provider impose strict limits on memory and CPU time may cause significant performance degradation, and the Heartbeat API may come into play.

In this guide, we will reveal the potential of the WordPress Heartbeat API and its role in the WordPress ecosystem. You will understand how the Heartbeat API works. You will also learn why you may need to consider restricting their activities on your WordPress website. This will help you get better performance without having to upgrade your hosting plan.

article directories

What is the WordPress Heartbeat API?

The WordPress Heartbeat API is an interface built into the core of WordPress that enables real-time communication between the server and the WordPress dashboard. The Heartbeat API was first introduced in 2013 with the release of WordPress 3.6. It makes significant improvements in other key areas of content management and website management.

WordPress心跳API

Why is the Heartbeat API so important?

The WordPress dashboard is primarily a client application. This means that most operations occur in the user’s browser. As a front-end application, the WordPress Management Area does not have direct access to the WordPress database that stores website content and other critical information.

In order to function properly, the WordPress dashboard needs to be able to communicate with the server to send and receive data. Application programming interfaces (APIs) such as the WordPress Heartbeat API make it possible to receive updates in real time without having to reload the entire page to display any new information.

To provide a seamless user experience, the WordPress dashboard sends requests to backend servers frequently enough to eliminate any update delays for WordPress users who are logged in and working on the site. The Heartbeat API will send requests and API calls to the server every 15 to 120 seconds.

Three main features of the WordPress Heartbeat API

The WordPress Heartbeat API provides three key features that make website management easier and more convenient. These features are automatic saving, content locking and real-time dashboard notifications. They are particularly important for communities or collaboratively edited websites where multiple users are logged in and active at the same time.

1. Automatic saving and revision

Automatic saves and revisions are one of the main features provided by the WordPress Heartbeat API. Whenever a user works in the block editor, WordPress performs an automatic content save every 60 seconds. This ensures that no changes are unsaved. By sending requests to the server every minute, the WordPress Heartbeat API saves a working copy of a post or page into its database, where the copy will be stored until a new automatic save is run.

WordPress automatic saving supported by the Heartbeat API is very useful. They prevent data loss if the browser crashes or connection problems. Automatic saves do not replace any revisions created by users and do not accumulate in the database.

2. content locking

Content locking is another feature of the WordPress Heartbeat API that can provide a better editing experience for WordPress website owners. If a post or page is currently being edited, WordPress will lock it out to prevent other users from making changes to it. The WordPress Heartbeat API will send a request to the server every 15 seconds. If another user opens locked content for editing, it displays a notification.

3. Real-time dashboard notifications

The WordPress Heartbeat API supports real-time dashboard notifications, including notifications sent by plug-ins. The reliability of client-server communication provided by the Heartbeat API has led thousands of WordPress plug-in developers to integrate it into their applications.

The default interval for WordPress Heartbeat API calls in the website management area is 15 seconds. Plug-in developers who want to use API capabilities can configure custom intervals to exchange data between client applications and servers.

How does the WordPress Heartbeat API work?

The WordPress Heartbeat API uses AJAX (asynchronous JavaScript and XML) to communicate with the server from the management dashboard. The application programming interface collects data and sends it through so-called jQuery events. The admin-ajax handler receives the data and processes it on the server side, then prepares and returns a response in JSON format.

When the WordPress dashboard is loaded, client code sets an interval, also known as a tick, that runs every 15 to 120 seconds, depending on the task. The task of the Heartbeat API code is to collect data on the client side and transfer it to the server to receive updates.

The total number of Heartbeat API calls depends on the number of active users working on the website at the same time. This is the same as the number of WordPress dashboard instances loaded. Opening the WordPress management area in multiple browser windows creates the same number of Heartbeat API calls, just like multiple users logging in to the back end of a website and performing tasks at the same time.

Why restrict WordPress Heartbeat API calls?

Limiting WordPress Heartbeat API calls can help speed up WordPress sites that allocate a very limited pool of server resources, especially in terms of CPU time. While the Heartbeat API is unlikely to be the key issue behind poor website performance, it is definitely worth considering.

Every time the Heartbeat API communicates with the server, a POST HTTP request is issued to the admin-ajax.php file in wp-admin. Since Heartbeat API calls are sent to the server every 15-120 seconds, if there are multiple active administrator users, the number of calls will increase, and the API can overload the server, resulting in excessive CPU and system memory utilization.

Some WordPress website owners may even mistake Heartbeat API activity for an ongoing DDOS attack. WordPress Heartbeat API requests cannot be cached, so each call consumes a certain amount of server resources. If the server proactively uses its resources to process API calls, your customers may experience content rendering delays while browsing your website.

Three things to consider before disabling the Heartbeat API

Completely disabling the Heartbeat API is rarely a good idea. In most cases, the WordPress dashboard will lose the ability to communicate with the server in real time. After you disable the Heartbeat API, you will no longer be able to receive notifications. In addition, you will not be able to take advantage of content locking, automatic saving and revision.

Three key things to consider before disabling the Heartbeat API or restricting its activities are:

  • Custody.Check how much CPU time and system memory you allocate to your website. Is there a limit to the number of processes your website can create?
  • Time spent managing the website.How much time do you spend managing and publishing content? How many active users often work on both WordPress dashboards and back-end interfaces?
  • The traffic received every day.How many visitors does your WordPress website receive every day? Do you have an appropriate caching solution to reduce the load on your server?

Depending on the type of website and your current hosting settings, limiting the number of WordPress Heartbeat API calls will have a different impact on overall performance. If you are running a resource-intensive WordPress site where multiple users perform regular maintenance and publishing content, reducing the number of Heartbeat API calls can be very beneficial. It can also help you avoid increasing hosting costs to accommodate increasing traffic.

How to use plug-ins to manage the WordPress Heartbeat API

The easiest way to manage WordPress Heartbeat API requests is to use a WordPress plugin designed for this purpose. You can take advantage of a variety of different solutions. Heartbeat Control developed by WP Rocket is one of the best and most popular.

Note: If you have already used the WP Rocket caching plug-in and have built-in Heartbeat API functionality, you no longer need to use a separate plug-in.

Install and activate the Heartbeat Control plug-in from the WordPress dashboard. navigate toSet heartbeat controlTo open the configuration of the plug-in. Heartbeat Control allows you to manage Heartbeat API activity in the website front end, WordPress dashboard, and block editor using the following options:

  • Allow the heartbeat.If this option is enabled, the plug-in will not restrict Heartbeat API calls in any way.
  • Disable heartbeat.Disabling Heartbeat will block all API calls in selected areas of the WordPress website.
  • Modify the heartbeat.You can set a custom interval for WordPress Heartbeat API calls, ranging from 15 to 300 seconds.
心跳控制

Most of the time, it is best to configure a larger interval for WordPress Heartbeat API calls. For example, limiting the number of API calls to one every five minutes in all three areas of the website has the same impact on overall performance as completely disabling the Heartbeat API.

Please note that limiting Heartbeat API activity in the Block Editor may require you to save revisions more frequently to avoid losing any changes to what you are editing. If you don’t rely on autosave and don’t need content locking, you can completely disable the WordPress Heartbeat API in Gutenberg.

How to restrict WordPress Heartbeat API calls without a plugin

You can use WordPress hooks to restrict WordPress Heartbeat API calls without plug-ins. Adding a few lines of code to your website’s active theme functions.php file can help you disable the Heartbeat API or specify a custom interval for API calls.& nbsp;

WordPress comes with a built-in theme editor that allows you to modify functions.php from the administrative dashboard. However, most WordPress security plug-ins disable this feature to protect your website from threats.

You can edit files using the file manager interface provided by the host control panel or connecting to your website files via FTP or SSH.

Note that any code added to the functions.php file will likely be overwritten by the next update to the theme. Therefore, it might be better to use plug-ins to manage the WordPress Heartbeat API. If you use a subtheme, you can change its functions.php file without risking being overwritten when the parent theme is updated.

Disable Heartbeat API

Adding the following action allows you to stop the Heartbeat API, effectively disabling all API calls. To do this, add the following code fragment to the bottom of functions.php.

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

You can disable specific features in the WordPress Heartbeat API in the same way.

function wpdx_disable_autosave() {
 wp_deregister_script('autosave');
}
add_action('admin_init', 'wpdx_disable_autosave');

Configure custom intervals for Heartbeat API calls

To limit WordPress Heartbeat API calls, you can specify a filter that will configure a custom interval for API requests. The following code fragment limits Heartbeat API calls to one request per minute.

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

Configure custom intervals for WordPress autosaves

WordPress allows you to configure custom intervals for automatic saves by redefining the AUTOSAVE_INTERVAL constant. Similar to other constants, you can assign new values to them by adding the following lines to the main WordPress configuration file:

define('AUTOSAVE_INTERVAL', 300);

You can assign a larger value to the interval, such as 3600, or one hour, to completely disable WordPress automatic saving. WordPress constants configured in wp-config.php will not be modified during any WordPress update. You don’t have to redefine them later.

summary

WordPress’s Heartbeat API is one of the key features built into the core of WordPress and allows real-time client-server communication using AJAX calls. The application programming interface allows WordPress to exchange data between the server and the WordPress dashboard to provide a better management experience.

The Heartbeat API sends requests to the server multiple times per minute. This can cause increased server load and performance degradation. You can limit Heartbeat API calls by using plug-ins, adding WordPress hooks, or defining WordPress constants.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注