<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: TivneT</title>
    <description>The latest articles on DEV Community by TivneT (@tivnet).</description>
    <link>https://dev.to/tivnet</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F202541%2F4bf40d7c-0f25-4cac-9754-b0b866b9244c.png</url>
      <title>DEV Community: TivneT</title>
      <link>https://dev.to/tivnet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tivnet"/>
    <language>en</language>
    <item>
      <title>WordPress debugging: disable heartbeats</title>
      <dc:creator>TivneT</dc:creator>
      <pubDate>Tue, 07 Feb 2023 13:07:18 +0000</pubDate>
      <link>https://dev.to/tivnet/wordpress-debugging-disable-heartbeats-3il4</link>
      <guid>https://dev.to/tivnet/wordpress-debugging-disable-heartbeats-3il4</guid>
      <description>&lt;h2&gt;
  
  
  Takeaways:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;WordPress heartbeats &lt;strong&gt;disturb debugging&lt;/strong&gt; by issuing regular AJAX requests.&lt;/li&gt;
&lt;li&gt;Heartbeats &lt;strong&gt;can be disabled&lt;/strong&gt; with a PHP code or by using a special WordPress plugin.&lt;/li&gt;
&lt;li&gt;Heartbeats are required for normal WordPress and WooCommerce operation, so &lt;strong&gt;disable them only temporarily&lt;/strong&gt;, for debugging.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Background:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are heartbeats in WordPress?
&lt;/h3&gt;

&lt;p&gt;Heartbeats is an essential feature of the WordPress administration.&lt;/p&gt;

&lt;p&gt;To provide auto-saving, post locking, login expiration warnings, etc. WordPress sends regular AJAX calls, "heartbeats" from the browser to the server.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to debug PHP code in WordPress?
&lt;/h3&gt;

&lt;p&gt;As a PhpStorm user, to debug a WordPress website, I first install and activate Xdebug.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The official instructions for debugging WordPress with PhpStorm can be found on the JetBrains website:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.jetbrains.com/help/phpstorm/debugging-wordpress.html" rel="noopener noreferrer"&gt;Debugging WordPress with PHPStorm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the problem with heartbeats?
&lt;/h2&gt;

&lt;p&gt;Once PHPStorm and Xdebug set up, I can inspect variables and step through the code during the paused execution at the breakpoints.&lt;br&gt;
However, every so and so seconds, my debugging screen opens a new tab showing a heartbeat request:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa0vcs3o6tw7v72rvccgr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa0vcs3o6tw7v72rvccgr.png" alt="Image description" width="800" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is annoying, consumes memory and slows down the website and PHPStorm IDE.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What can be done?
&lt;/h2&gt;

&lt;p&gt;To stop heartbeats, place this to your WordPress theme's &lt;code&gt;functions.php&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add_action(
    'init',
    function () {
        wp_deregister_script( 'heartbeat' );
    }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a couple of issues with this simple method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The post autosave will fail, causing PHP notices and JavaScript errors.&lt;/li&gt;
&lt;li&gt;When you push the code to production, you might forget to remove that code snippet and break the website operation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;A solution&lt;/strong&gt; would be to place the code to a custom plugin and never push that plugin to the live site.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A better way&lt;/strong&gt; would be using a 3rd party plugin that has that code in place and also takes care of the side effects.&lt;/p&gt;

&lt;p&gt;Here is a popular plugin that you can try:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://wordpress.org/plugins/heartbeat-control/" rel="noopener noreferrer"&gt;Heartbeat Control&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If your site is running &lt;strong&gt;WooCommerce&lt;/strong&gt;, you might consider this premium extension, which provides multiple administration tools, including stopping heartbeats:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://woocommerce.com/products/admin-tools-for-woocommerce/" rel="noopener noreferrer"&gt;Admin Tools for WooCommerce&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devto</category>
      <category>announcement</category>
      <category>offers</category>
    </item>
  </channel>
</rss>
