Disable the Heartbeat API Except in the Post Editor
Keeps the Heartbeat API (autosave and post locking) in the editor but turns it off on every other admin and front-end page to save server requests.
Opens WPIDE on
Code
add_action( 'admin_enqueue_scripts', function ( $hook ) {
if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) {
wp_deregister_script( 'heartbeat' );
}
}, 1 );
add_action( 'wp_enqueue_scripts', function () {
wp_deregister_script( 'heartbeat' );
}, 1 );
Notes
Some plugins rely on Heartbeat on their own screens; and the "session expired" login prompt only appears in the editor.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.