1. Knowledge Base
  2. >
  3. Results
  4. >
  5. WordPress - Heartbeat

WordPress - Heartbeat

What is Heartbeat?

The Heartbeat API included in WordPress version 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 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 log in to your WordPress Dashboard and minimize your browser while you worked on other tasks, the API would by default still be updated 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.

Opening and Editing Files

Note: The plugins.php file is located in the wp-admin folder under your document root. The location of the functions.php file depends on the theme used, and information on finding this file is given below.

Opening the File Manager and Locating the Target File   

  1. Log in to cPanel.    
  2. In the Files section, click on the File Manager icon.

    cPanel File Manager

  3. Check the box for Document Root and select the domain name you wish to access from the dropdown menu.   
  4. Find and double-click your target folder icon. (You may also navigate to the target folder using the directory tree of folders in the left sidebar.)  
  5. In the folder, locate the target file. You may need to scroll to find it.

Editing a File    

  1. Right-click on the file you wish to edit. Click Edit from the menu. (You can also do this by highlighting the icon for the target file, then click on the Editor icon at the top of the page.)    
  2. A dialogue box may appear asking you about encoding. Just click Edit to continue. The editor will open in a new tab.    
  3. Edit the file as suggested.    
  4. Click Save Changes in the upper right-hand corner when done. The changes will be saved.    
  5. Test your website to make sure your changes were successfully saved. If not, correct the error or revert to the previous version until your site works again.  
  6. Once complete, you can click Close to close the File Manager window.

Disable Heartbeat Through plugins.php File

This method of disabling the Heartbeat API is the simplest to apply and 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:

File Manager Edit plugins.php

File Manager Edit plugins.php

add_action( 'init', 'kill_heartbeat', 1 ); function kill_heartbeat() { global $pagenow; wp_deregister_script('heartbeat'); } 

File Manager Edit plugins.php

Note: The path for the plugins.php file may differ depending on where you have WordPress installed. The file would be located here in a typical installation: /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:

File Manager Edit functions.php

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 with 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.

Towards the top of the functions.php file, add the following code to disable the Heartbeat API everywhere:

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

Disable WordPress Heartbeat API for WordPress Dashboard Only

The Heartbeat API enables users to customize every moment of a visitor's 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.

Add the following code into your functions.php file:

add_action( 'init', 'stop_heartbeat', 1 ); function stop_heartbeat() { global $pagenow; if ( $pagenow == 'index.php' ) wp_deregister_script('heartbeat'); } 

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 the code that will be provided to you. This will allow the browser to send updates to the server when writing or to edit a post to maintain the autosave and post locking features.

Add the following code into your functions.php file:

add_action( 'init', 'stop_heartbeat', 1 ); function stop_heartbeat() { global $pagenow; if ( $pagenow != 'post.php' && $pagenow != 'post-new.php' ) wp_deregister_script('heartbeat'); } wp_deregister_script('heartbeat'); } 

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 have been 5 minutes of inactivity within the browser.) Click on the link for instructions on how to change the interval.

Add the following code to your functions.php file to change the time between updates within the Heartbeat API. By default, the time between updates for an active page is 15 seconds. The code below will change this interval to 60 seconds.
add_filter( 'heartbeat_send', 'my_heartbeat_settings' ); function my_heartbeat_settings( $response ) { if ( $_POST['interval'] != 60 ) { $response['heartbeat_interval'] = 60; } return $response; }