The WordPress Heartbeat API is responsible for sending periodic requests between the browser and the server to check for updates, such as post autosaves and plugin/theme updates. While this API can be useful, it can also consume server resources, especially on sites with heavy traffic or on shared hosting environments. In some cases, disabling the Heartbeat API can help improve the performance of your WordPress site.
To disable the WordPress Heartbeat API, you can add the following code snippet to your theme’s functions.php
file or in a custom plugin:
function wpsnippets_disable_heartbeat() {
wp_deregister_script('heartbeat');
}
add_action('init', 'wpsnippets_disable_heartbeat', 1);
This code snippet uses the wp_deregister_script()
function to deregister the heartbeat
script, effectively disabling the Heartbeat API. The add_action()
function is used to hook into the init
action with a priority of 1
, ensuring that the script is deregistered early in the WordPress initialization process.
By adding this code snippet, the Heartbeat API will be disabled on your WordPress site, reducing the server load caused by frequent AJAX requests. This can be particularly useful if you’re experiencing high CPU usage or slow performance due to the Heartbeat API.
Note: Disabling the Heartbeat API may affect certain features in WordPress, such as real-time updates in the post editor and the display of other users editing the same post. Therefore, it’s important to test your site thoroughly after disabling the Heartbeat API to ensure that all desired functionality is still working as expected.
Examples
Example #1: Disable Heartbeat API on the Frontend
This use case demonstrates how to disable the WordPress Heartbeat API on the frontend of your website. The Heartbeat API is responsible for real-time communication between the browser and the server, but it can sometimes cause high CPU usage. By disabling it on the frontend, you can reduce the server load and improve the performance of your website.
function wpsnippets_disable_heartbeat_frontend() {
wp_deregister_script('heartbeat');
}
add_action('wp_enqueue_scripts', 'wpsnippets_disable_heartbeat_frontend', 1);
This code example uses the wp_deregister_script()
function to deregister the Heartbeat API script on the frontend. It hooks into the wp_enqueue_scripts
action with a priority of 1 to ensure it runs early in the script loading process.
Example #2: Disable Heartbeat API on the Admin Dashboard
This use case demonstrates how to disable the WordPress Heartbeat API on the admin dashboard. The Heartbeat API is used in the admin area for various tasks like autosaving, post locking, and revision tracking. However, it can consume server resources, especially on busy sites. Disabling it on the admin dashboard can help alleviate the server load.
function wpsnippets_disable_heartbeat_admin() {
wp_deregister_script('heartbeat');
}
add_action('admin_enqueue_scripts', 'wpsnippets_disable_heartbeat_admin', 1);
This code example is similar to the previous one, but it hooks into the admin_enqueue_scripts
action instead. By deregistering the Heartbeat API script, it prevents it from being loaded on the admin dashboard.
Example #3: Disable Heartbeat API Completely
This use case demonstrates how to completely disable the WordPress Heartbeat API on both the frontend and the admin dashboard. If you don’t require any of the functionality provided by the Heartbeat API, disabling it entirely can help reduce server load and improve performance.
function wpsnippets_disable_heartbeat_completely() {
wp_deregister_script('heartbeat');
wp_deregister_script('jquery');
}
add_action('init', 'wpsnippets_disable_heartbeat_completely', 1);
This code example goes a step further by not only deregistering the Heartbeat API script but also the jQuery script. The Heartbeat API relies on jQuery, so by deregistering it, you effectively disable the Heartbeat API completely. It hooks into the init
action with a priority of 1 to ensure it runs early in the WordPress initialization process.