search ]

Optimize Heartbeat API Settings with WP Rocket

Everyone who reads my blog knows I have OCD when it comes to optimizing and improving the speed of WordPress sites. If you’ve stumbled upon this article, chances are you’re trying to figure out why your WordPress site is slow.

After implementing various speed optimization tips, you may have concluded that the culprit is the file admin-ajax.php.

But why is this file causing performance issues? In this article, I’ll explain the root cause of the problem and show you how to control it effectively using WP Rocket.

There are two main issues with admin-ajax.php:

  • WordPress Core: Excessive, and often unnecessary, use of the server’s CPU caused by core functionality.
  • Third-Party Plugins: Plugins that trigger excessive AJAX calls through admin-ajax.php, often leading to degraded frontend performance.

Understanding admin-ajax.php and the Heartbeat API

In WordPress version 3.6, the WordPress Heartbeat API was introduced to enable communication between the browser and the server.

This API facilitates session management, post revision tracking, and automatic draft saving in WordPress.

The Heartbeat API uses the /wp-admin/admin-ajax.php file to send AJAX calls from the browser. While this is useful for real-time updates in the admin dashboard, it can result in excessive POST requests that overburden your server’s CPU and generate unnecessary PHP calls.

Example: Imagine having multiple tabs open in your browser, all using the Heartbeat API. Each tab sends requests to admin-ajax.php at regular intervals (every 15 seconds by default), potentially overloading your server.

By default, the Heartbeat API sends a request every 15 seconds. WordPress allows you to adjust this interval to any value between 15 and 120 seconds using the heartbeat_settings filter. On busy sites with multiple editors, this default can quickly add up to hundreds of requests per minute.

Important: The Heartbeat API is not the only cause of high admin-ajax.php usage. WooCommerce cart fragments (wc-ajax=get_refreshed_fragments), live search plugins, and page builders can also generate excessive AJAX requests. Use a tool like Query Monitor to identify which actions are responsible.

Optimizing Heartbeat API Settings with WP Rocket

Optimizing Heartbeat API Settings with WP Rocket

WP Rocket provides built-in settings to manage and optimize the Heartbeat API, allowing you to control its behavior without writing custom code. Here’s how you can use WP Rocket to manage the Heartbeat API:

Step 1: Navigate to Heartbeat Settings

In your WordPress dashboard, go to Settings > WP Rocket > Heartbeat. Here, you’ll find options to control the Heartbeat API for different areas of your site.

Step 2: Adjust Heartbeat Behavior

WP Rocket offers three main options to manage the Heartbeat API:

  • Reduce Activity: Lowers the request frequency from once per minute to once every two minutes. This is the recommended option for balancing performance and functionality.
  • Disable: Completely stops Heartbeat API activity in the selected area. Use this only if you’re confident it won’t disrupt essential functionality, such as autosave.
  • Do Not Limit: Leaves the Heartbeat API at its default frequency. Choose this for areas where real-time updates are critical.

You can apply each of these options independently to three areas:

  • Dashboard: Controls Heartbeat API activity in the WordPress admin dashboard.
  • Post Editor: Controls Heartbeat API activity on post and page editing screens.
  • Frontend: Controls Heartbeat API activity for any frontend AJAX calls.

Step 3: Choose the Best Settings

For most WordPress sites, I recommend reducing the activity rather than disabling the Heartbeat API entirely. Here’s an example configuration:

  • Dashboard: Reduce activity or disable, as this area doesn’t often require frequent updates.
  • Post Editor: Set to reduced activity to retain autosave and collaboration features.
  • Frontend: Disable entirely unless specific plugins rely on frontend AJAX calls via Heartbeat.

This configuration strikes a balance between improving performance and maintaining essential features like autosave or plugin functionality.

How WP Rocket Improves Performance

By reducing the frequency or disabling the Heartbeat API in unused areas, WP Rocket significantly minimizes the number of requests sent to admin-ajax.php. This reduction directly decreases server load, enhances CPU efficiency, and improves Time to First Byte (TTFB).

For instance, setting the Heartbeat interval to 60 seconds instead of the default 15 seconds can drastically cut down on unnecessary requests, especially on sites with multiple editors or heavy admin usage.

Controlling Heartbeat with Code

If you prefer not to use a plugin, you can control the Heartbeat API directly with PHP. WordPress provides the heartbeat_settings filter to adjust the interval, and you can use wp_deregister_script to disable it entirely on specific pages.

To change the Heartbeat interval to 60 seconds, add this snippet to your theme’s functions.php file or a custom plugin:

add_filter( 'heartbeat_settings', function( $settings ) {
    $settings['interval'] = 60; // Value between 15 and 120 seconds
    return $settings;
} );

To disable the Heartbeat API on the frontend only:

add_action( 'init', function() {
    if ( ! is_admin() ) {
        wp_deregister_script( 'heartbeat' );
    }
} );

These snippets give you fine-grained control without adding another plugin to your site.

Analyzing Plugins Using admin-ajax.php

In addition to optimizing the Heartbeat API, it’s important to identify plugins that overuse admin-ajax.php.

Use Chrome Developer Tools (Network tab, filter by “admin-ajax”) or the Query Monitor plugin to inspect AJAX requests and pinpoint the plugins responsible for excessive server calls. This will help you decide whether to configure, replace, or selectively disable those plugins.

Note: Completely disabling the Heartbeat API in the WordPress backend will prevent the Site Health tool from loading its results. If you rely on Site Health checks, use “Reduce Activity” instead of “Disable” for the Dashboard area.

FAQs

Common questions about the WordPress Heartbeat API and admin-ajax.php optimization:

What is the WordPress Heartbeat API?
The Heartbeat API is a built-in WordPress feature (since version 3.6) that enables real-time communication between the browser and the server. It powers autosave, post locking (to prevent editing conflicts), session expiration warnings, and real-time dashboard updates. It works by sending periodic AJAX requests to admin-ajax.php.
What is the default Heartbeat API interval?
The default Heartbeat API interval is 15 seconds. WordPress allows you to adjust it to any value between 15 and 120 seconds using the heartbeat_settings filter. Most performance-focused configurations recommend setting it to 60 seconds.
Is it safe to disable the Heartbeat API completely?
Disabling the Heartbeat API completely is generally not recommended. It will break autosave, post locking, and session management. Disabling it in the backend also prevents the WordPress Site Health tool from loading results. Instead, reduce the frequency or disable it only on the frontend where it is rarely needed.
Can I control the Heartbeat API without WP Rocket?
Yes. You can use the heartbeat_settings PHP filter to change the interval, or use wp_deregister_script('heartbeat') to disable it on specific pages. Free plugins like Heartbeat Control and Perfmatters also offer one-click heartbeat management.
Why is admin-ajax.php using so much CPU even after optimizing Heartbeat?
The Heartbeat API is only one source of admin-ajax.php requests. Other common causes include WooCommerce cart fragments, live search plugins, page builders, popup plugins, and even bot spam targeting the endpoint. Use Chrome Developer Tools or the Query Monitor plugin to identify which actions are generating the most requests.

Conclusion

Managing the WordPress Heartbeat API is essential for maintaining optimal performance, particularly on busy sites. WP Rocket provides an intuitive way to control the Heartbeat API, offering flexible options to reduce server load while preserving necessary functionality.

If you prefer more control, the heartbeat_settings PHP filter lets you fine-tune the interval without any plugin. Whichever method you choose, reducing unnecessary admin-ajax.php requests is one of the easiest wins for WordPress performance.

Related Articles on WordPress Speed Optimization:

Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo