Takeaways:
- WordPress heartbeats disturb debugging by issuing regular AJAX requests.
- Heartbeats can be disabled with a PHP code or by using a special WordPress plugin.
- Heartbeats are required for normal WordPress and WooCommerce operation, so disable them only temporarily, for debugging.
Background:
What are heartbeats in WordPress?
Heartbeats is an essential feature of the WordPress administration.
To provide auto-saving, post locking, login expiration warnings, etc. WordPress sends regular AJAX calls, "heartbeats" from the browser to the server.
How to debug PHP code in WordPress?
As a PhpStorm user, to debug a WordPress website, I first install and activate Xdebug.
Then I configure PhpStorm to listen for Xdebug connections, set breakpoints in the WordPress code, and start a debugging session by visiting the website with a special cookie set.
The official instructions for debugging WordPress with PhpStorm can be found on the JetBrains website:
Debugging WordPress with PHPStorm
It provides a comprehensive guide on how to set up and use PhpStorm for debugging WordPress, including the installation and activation of Xdebug, configuring PhpStorm, and starting a debugging session.
What is the problem with heartbeats?
Once PHPStorm and Xdebug set up, I can inspect variables and step through the code during the paused execution at the breakpoints.
However, every so and so seconds, my debugging screen opens a new tab showing a heartbeat request:
This is annoying, consumes memory and slows down the website and PHPStorm IDE.
What can be done?
To stop heartbeats, place this to your WordPress theme's functions.php
:
add_action(
'init',
function () {
wp_deregister_script( 'heartbeat' );
}
);
There are a couple of issues with this simple method:
- The post autosave will fail, causing PHP notices and JavaScript errors.
- When you push the code to production, you might forget to remove that code snippet and break the website operation.
A solution would be to place the code to a custom plugin and never push that plugin to the live site.
A better way would be using a 3rd party plugin that has that code in place and also takes care of the side effects.
Here is a popular plugin that you can try:
If your site is running WooCommerce, you might consider this premium extension, which provides multiple administration tools, including stopping heartbeats:
Top comments (0)