how to disable WordPress heartbeat api

The WordPress Heartbeat API is a vital feature that enables your browser to communicate with the server while logged into the WordPress admin panel. However, despite its benefits in real-time data syncing and user session management, it can lead to high CPU usage and other resource strains, particularly on shared hosting environments. Here we delve into methods to disable or control the Heartbeat API to optimize website performance.

Understanding the Heartbeat API

The API operates by sending periodic AJAX calls to the server at regular intervals. These AJAX calls occur every 15-60 seconds, depending on your task and location within WordPress. For example, post editors experience more frequent communication to manage autosaves and revision controls.

Why Disable the Heartbeat API?

While beneficial, these calls can be resource-intensive and may significantly impact website speed and server performance. Disabling the API helps reduce the workload on your server, which can be crucial for high-traffic sites.

Method 1: Using a Plugin

One of the simplest ways to manage the Heartbeat API is by using a plugin. Plugins like “Heartbeat Control by WP Rocket” provide an interface to manage the frequency of the heartbeat or disable it entirely.

  1. Install and Activate the Plugin: Go to your WordPress dashboard, navigate to ‘Plugins’ > ‘Add New’, and search for “Heartbeat Control by WP Rocket”. Install and activate the plugin.
  2. Configure Settings: Once activated, go to ‘Settings’ > ‘Heartbeat Control’ and configure the settings. You can disable the Heartbeat API across the site, or you can specify areas (Dashboard, Frontend, or Post Editor) where you want to disable or modify its frequency.

Method 2: Using Code in functions.php

For those who prefer not to use a plugin, adding code to the functions.php file of your theme is an effective alternative.

  1. Access Your Theme’s Functions.php File: This can be done via FTP or through the ‘Editor’ section under ‘Appearance’ in the WordPress dashboard.

  2. Add Code to Disable Heartbeat: Paste the following code snippet at the end of the functions.php file:

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

This simple code tells WordPress to stop the Heartbeat API script from running, effectively disabling it.

Method 3: Modifying the Heartbeat API Using the wp-config.php File

This method involves defining rules in the wp-config.php file to limit or disable the Heartbeat API.

  1. Access Your wp-config.php File: Like the functions.php method, access this file through FTP or File Manager in your hosting control panel.

  2. Add Configuration Code: Above the “/ That’s all, stop editing! Happy blogging. /” line, insert the following:

    define('DISABLE_WP_CRON', true);
    define('WP_HEARTBEAT_INTERVAL', 60); // Sets Heartbeat interval to 60 seconds

With WP_HEARTBEAT_INTERVAL, you can increase the interval time to reduce the frequency of the API’s server requests.

Method 4: Limiting or Disabling Heartbeat in Specific Locations

Particularly useful in multi-author environments, focusing Heartbeat control in certain parts of your site can optimize performance without fully disabling the feature.

  1. Using Code in functions.php: To specifically disable the API on the post edit page and keep it elsewhere, use:

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

Or, to control it based on role, introduce a conditional check with current_user_can() within your added function.

Conclusion

Controlling or disabling the WordPress Heartbeat API allows website administrators to balance functionality and resource usage efficiently. Whether through plugins or coding, managing this feature can lead to enhanced server performance, ultimately fostering a better user experience and improved site stability. Play around with different settings to find what works best for your particular WordPress environment and workload.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *