Control WordPress Heartbeat API
- Performance
The Heartbeat API fires AJAX requests every 15-60 seconds to keep sessions alive, autosave posts, and detect simultaneous editing. On sites with many admin users, this can overwhelm the server with constant admin-ajax.php requests.
<?php
/**
* Control WordPress Heartbeat API
*
* The Heartbeat API fires an admin-ajax request every 15-60 seconds on
* every admin page. This keeps the server busy with requests that are only
* useful on the post editor (for autosave and lock detection). This snippet
* disables Heartbeat everywhere except the post editor and slows it down
* there to reduce server load.
*
* Usage: Add to functions.php.
*
* @category performance
* @language php
*
* @author Adame Dahmani https://adame.io
* @support Support my family project: https://adame.io/fundraiser
*/
add_action( 'init', function () {
if ( ! is_admin() ) {
wp_deregister_script( 'heartbeat' );
return;
}
global $pagenow;
if ( $pagenow !== 'post.php' && $pagenow !== 'post-new.php' ) {
wp_deregister_script( 'heartbeat' );
}
}, 1 );
add_filter( 'heartbeat_settings', function ( $settings ) {
$settings['interval'] = 60;
return $settings;
} );This disables Heartbeat on the frontend entirely, keeps it only on the post editor (where autosave matters), and slows it to 60-second intervals. The result: fewer AJAX requests without losing autosave protection.