WordPress - Heartbeat


What is Heartbeat?

The Heartbeat API, included in WordPress versions 3.6 and later, is designed to provide fluid updates between your browser and the server to allow for autosaving, post locking, login expiration warnings, and much more. It works in the background to send a regular "pulse" between the browser and the server that is repeated at regular intervals and can be used to keep them in sync.

However, the downside of Heartbeat is that it can cause high CPU usage on the server. (In fact, even if you were to simply login to your WordPress Dashboard and minimize your browser while you worked on other tasks, the API would by default still be updating at regular intervals in the background.)

If you have experienced heavy CPU loads and are using a version of WordPress with Heartbeat included, you can follow the steps below to disable or manage the WordPress Heartbeat API to reduce your server load.

Please backup your files before making any changes suggested in this article. For information on how to generate a full backup, please refer to the following article:
HostGator does not provide direct support regarding any code in this article and it is provided for informational purposes only.
Note: Click any link below containing ▷ or ▽ to toggle more instructions.

Opening and Editing Files

Click on the link above for detailed instructions for opening and editing files.


Disable Heartbeat Through plugins.php File

This method of disabling the Heartbeat API is the simplest to apply and the one most recommended by HostGator. It will disable Heartbeat across your entire WordPress installation, even if the theme is changed at a later date. To apply, add the code found in this section to the bottom of your plugins.php file, found in the wp-admin directory of your WordPress installation:



At the bottom of the plugin.php file, add the following code to disable the Heartbeat API across your entire WordPress installation:
    
add_action( 'init', 'kill_heartbeat', 1 ); function kill_heartbeat() { global $pagenow; wp_deregister_script('heartbeat'); }
Note: The path for the plugins.php file may differ depending on where you have WordPress installed. In a typical installation, the file would be located here: /home/cpanelusername/public_html/wp-admin/plugins.php (Replace cpanelusername with your cPanel username.)

Disable Heartbeat Through Your Theme

If you wish to mitigate the resources used by the Heartbeat API in a more custom environment, you can do so by editing the functions.php file within your currently active theme. The following image illustrates where in this file you should input the provided code:

Note: The path for the functions.php file may differ depending on where you have WordPress installed. In a typical installation, the file would be located here: /home/cpanelusername/public_html/wp-content/themes/themename/functions.php

Replace cpanelusername with your cPanel username and themename with the name of your currently used WordPress Theme.

Disable WordPress Heartbeat API Everywhere

When trying to save on CPU resources, it may be worth considering the benefits of the features provided and determine if you need this API active at all. For sites that do not have multiple editors working on posts together, it may not be essential to have post locking enabled. If your posts are typically short and easily reproduced, it may not be worth consuming resources to enable autosaving. Clicking on the link above will provide you with code to disable this function across all areas of your WordPress installation.

Disable WordPress Heartbeat API for WordPress Dashboard Only

The Heartbeat API enables users to customize every moment of a visitors experience on their site. This includes the ability to use behind the scene server tasks to disable or enable a notice on the screen anytime a value in the database changes. If you do not want to lose this functionality, but still need to trim down the resources your script is using, click the link above and code to disable the API on your Dashboard only will be provided to you.

Disable WordPress Heartbeat API for WordPress Everywhere Except for post.php and post-new.php

The Heartbeat API for WordPress can be disabled everywhere except for posts (post.php and post-new.php). Click the link for code will be provided to you. This will allow the browser to send updates to the server when you are writing or editing a post to maintain the autosave and post locking features.

Change the Interval of WordPress Heartbeat API Updates

If you would like to keep all the features of the Heartbeat API but are battling high CPU loads, you may consider changing the update rate. By default, updating happens every 15 seconds for pages that are "focused" and every 2 minutes for pages that are "non-focused." (WordPress identifies a page as non-focused when there has been 5 minutes of inactivity within the browser.) Click on the link for instructions on how to change the interval.