WPSurfer.com

How to Disable Heartbeat in WordPress [Code Snippets]

Published on September 24, 2022 | Updated on July 7, 2024

Heartbeat allows for real-time post locking, which prevents multiple users from editing the same post simultaneously.

If another user tries to edit a post that you are already editing, they will receive a notification.

While the Heartbeat API is useful, it can also increase server load due to the frequent AJAX calls, especially on high-traffic sites or shared hosting environments. This can sometimes lead to performance issues, which is why some users choose to disable or limit the Heartbeat API.


How to Disable Heartbeat in WordPress

Whether you use a code snippets plugin or the function.php file from a child theme, add this code to disable the heartbeat altogether.

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

If you rather use mu-plugins for custom PHP, create a PHP file and add the following code to it:

<?php
/*
Plugin Name: Disable WordPress Heartbeat
Description: A must-use plugin to disable the WordPress Heartbeat API.
Author: Your Name
Version: 1.0
*/

// Disable WordPress Heartbeat
add_action('init', 'stop_heartbeat', 1);
function stop_heartbeat() {
    wp_deregister_script('heartbeat');
}

How to Customize Heartbeat API

If you don’t want to completely disable the Heartbeat API but want to reduce its frequency.

Add this code to your code snippets plugin or to your functions.php file

// Adjust Heartbeat API frequency
add_action('init', 'adjust_heartbeat_frequency');
function adjust_heartbeat_frequency() {
    add_filter('heartbeat_settings', 'customize_heartbeat_settings');
}

function customize_heartbeat_settings($settings) {
    $settings['interval'] = 60; // Change this value to the desired interval in seconds
    return $settings;
}

If you use mu-plugins, you should try this:

<?php
/*
Plugin Name: Adjust Heartbeat API Frequency
Description: A must-use plugin to adjust the Heartbeat API frequency in WordPress.
Author: TicoLibre
Version: 1.0
*/

// Adjust Heartbeat API frequency
add_action('init', 'adjust_heartbeat_frequency');
function adjust_heartbeat_frequency() {
    add_filter('heartbeat_settings', 'customize_heartbeat_settings');
}

function customize_heartbeat_settings($settings) {
    $settings['interval'] = 60; // Change this value to the desired interval in seconds
    return $settings;
}

A Plugin for Disabling and Customizing the WordPress Heartbeat

If using code snippets and creating mu-plugin is not your thing, you can find free plugins that let you disable of modify the WordPress Heartbeat.

Most Speed optimization plugins will let you do that.

If you need a free plugin, consider using Heartbeat Control by WPRocket.

The plugin has option to disable or customize the WordPress Heartbeat at three levels:

#1WordPress Dashboard
#2Front-End
#3Post-Editor

Manuel Campos

Manuel Campos

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.

WP SURFER

home

about

privacy

contact

© 2024 WP SURFER • Made with Love in Costa Rica