How to disable WordPress heartbeat API to reduce CPU usage

,

How to disable WordPress heartbeat API to reduce CPU usage

WordPress is the standard choice for online content management systems. People who are familiar with CPanel software and Apache servers know how easy it is to install WordPress using either 1Click Installer or manually (requires bit more time and effort) . WordPress is by default a fairly optimized PHP based CMS. But still with time and new additions some of the features of WordPress can be very CPU intensive, specially for shared hosting servers or for those people who are using free servers. One of these new features is the WordPress heartbeat. The heartbeat is essentially a background script which keeps running and is an integral part of WordPress’ functionality. Let’s know more about it and learn how to disable WordPress heartbeat API to reduce CPU usage.

What is WordPress heartbeat API?

The name Heartbeat comes from the fact that similar to a beating heart which generates a pulse the Heartbeat API also generates a pulse i.e. a tick which is associated with different actions which are performed periodically. Heartbeat API is JavaScript based and it allows WordPress to have cross communication between the server and browser. It was initially introduced in WordPress 3.6, it helps in post revision tracking, auto saving of posts while writing (that handy auto draft feature) and user session management.

How does the Heartbeat API work?

The script /wp-admin/admin-ajax.php is used by the WordPress heartbeat API to run AJAX calls. This is very useful as it helps WordPress to keep track of everything being done on the dashboard. But the dark side is that it keeps sending requests even when the page might not be in use.

Where does the problem start?

The problem starts when the Heartbeat API starts calling the admin-ajax.php file frequently, this can lead to high CPU usage.

Even when someone has only logged into WordPress admin dashboard and is working on other tabs or they have minimized the active window then also the admin-ajax.php file is requested repeatedly after a fixed interval of time.

The number of script executions even when doing nothing is quite high and can lead to CPU resource over-usage or further problems. A simple solution to this problem is to disable or selectively disable the WordPress heartbeat API.

How to disable WordPress heartbeat API to reduce CPU usage : The method.

To disable the Heartbeat API you have to locate the functions.php file of your active theme first. For doing so follow these steps :

  • After logging into WordPress dashboard go to Appearance > Editor from the menu on the left hand side.
  • Now locate the functions.php file from the list of files on the right hand side.
  • The path to the file may look like : /home/username/public_html/wp-content/themes/currently_active_theme/functions.php
  • Make a local backup of this file in case anything goes wrong and you may need to revert to previous stable code.
  • Click on it to start editing it.

Now that you have functions.php and it’s ready for editing let’s see the possible courses of action :

NOTE : You can either completely disable WordPress Heartbeat API or you can choose to selectively disable it.

Steps to disable WordPress Heartbeat API completely :

Add the following code at the beginning of functions.php just after the <?php tag (opening php tag).

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

The above code disables heartbeat sitewide and many of the functions of WordPress that were normally available would not work when this code has been added. But this one promises the most reduced CPU usage. Currently, we also use this code and every time we are done writing a post then we remove it for sometime just to change the post URL before publishing and editing the tags. (These functions don’t work when Heartbeat API is disabled.)

Steps to disable WordPress Heartbeat only on Dashboard page (index.php) :

We make use of the global WordPress $pagenow variable to determine which page user is currently at. We can make use of a conditional if statement to define the pages on which the WordPress Heartbeat API should be enabled.

add_action( 'init', 'stop_heartbeat', 1 );
function stop_heartbeat() {
global $pagenow;
if ( $pagenow == 'index.php' )
wp_deregister_script('heartbeat');
}

The above code disabled the heartbeat only on WordPress dashboard page. (It would only make a moderate difference in CPU usage.)

Steps to keep WordPress Heartbeat only on post.php and post_new.php pages :

By selectively using if statements we can keep the Heartbeat alive on post.php and post_new.php pages where they are needed the most. ( This option will be most beneficial for most of the users.)

add_action( 'init', 'stop_heartbeat', 1 );
function stop_heartbeat() {
global $pagenow;
if ( $pagenow != 'post.php' && $pagenow != 'post-new.php' )
wp_deregister_script('heartbeat');
}

By adding the above code in the functions.php the Heartbeat API would be disabled everywhere except the post.php and the post_new.php pages. This is the most hassle free option where you won’t need to re-enable and disable it once you are done. But the resource usage will still be significant.

*We can tell this by experience as most of the times we left add new post pages open and went on to do other work, this resulted in frequent calls and CPU usage spikes.

NOTE : In order to maximize the efficiency of the above variant of enabling Heartbeat we would recommend you to stay logged in only when doing something significant. If you aren’t using WordPress then it would be a better choice to log out and focus on other work.

So, this was our How to disable WordPress heartbeat API to reduce CPU usage guide, did you like it? If yes, then do comment below.

If you run into some problems while using these codes then do comment below, at Inspire2rise help will always be given to those who ask for it! 🙂

For more awesome blogging posts, keep visiting Inspire2rise.com.

Stay Inspired to rise!

yeah inspire2rise
Better subscribe and keep visiting for more such awesome posts!

Follow Inspire2rise on Twitter. | Follow Inspire2rise on Facebook. | Follow Inspire2rise on Google+.

See more : 

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Written by Aditya Nath Jha

Aditya Nath Jha is an Engineer from New Delhi, India. His areas of interest include Gadgets, WordPress, speed optimization & latest technology.
When he is not busy blogging he loves to write poetry, compose his own songs and has a taste for music! Find me on Facebook, Google +, Twitter, Linked in.
And watch my videos on Youtube.

Comments

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading…

Facebook Comments

when indian bloggers meet by clicksbazaar featured

When Indian Bloggers meet the result is awesome

You don’t need to be an A-Lister to be a Kick Ass Blogger

You don’t need to be an A-Lister to be a Kick Ass Blogger