How To Optimize WordPress Heartbeat Without A Plugin

Heartbeat is an API owned by WordPress that allows WordPress developers to make use of AJAX in a unique way. The system automatically ticks or beats when a user is on the site to see a variety of things such as:

  • To see if the user is still there.
  • To see if something has changed.

While the Heartbeat API is useful, it is also quite costly and by default ticks every 15 seconds. Obviously, that is a lot and therefore this process uses a lot of server resources. To minimize the number of server resources used, the API can be optimized.

The most common solution is to install the Heartbeat Control plugin. While the plugin is useful, it tends to clutter the admin panel is not necessary.

Optimizing Heartbeat API

Today, I am going to show you code that can be added to your functions.php file and it will reduce the “tick” of the default heartbeat from 15 seconds to 60 seconds. This reduces the number of pings by 75%.

Add the following piece of code to your functions.php file. As stated above, this reduces the load on your server dramatically. This is especially true if your website’s host limits your CPU cycles.

function sert_heartbeat_settings( $settings ) {
 $settings['interval'] = 120; //Anything between 15-120
 return $settings;
}
add_filter( 'heartbeat_settings', 'sert_heartbeat_settings' );
scott hartley

About the author

Scott is a web performance geek, lover of all things coffee, and avid video game player. His WordPress work delves into web performance, web security, and SEO.

2 thoughts on “How To Optimize WordPress Heartbeat Without A Plugin”

  1. Hi,

    I’m quite interested in using this function to reduce the calls made by the Heartbeat API.

    Can you tell me if there’s a way to verify that the function is working? I’ve tried the following code before to completely disable the Heartbeat API and noticed it doesn’t work at all:

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

    So before adding the code you provided on all of my websites, I want a way to test if it actually works.

    Reply
    • So to test that code whether or not it’s working you would want to check your header to see if the heartbeat JS is loaded or not. Your function simply prevents the script from being enqueued at all so if you don’t see the JS loading then it’s working.

      As far as testing it to be certain the only thing I can imagine doing would be to add the code and then measuring the admin-ajax.php requests that are hitting the server before and after. It would need to be done in a fairly black box situation such as on a staging site.

      Reply

Leave a Comment