I know it is too late to take WordPress 3.6 topic which HeartBeat API but Better late than never.As WordPress version, 3.6 was introduced a great new API which is known as Heartbeat API.This API is mainly useful to track changes, user sessions, revisions and automatic save content at some interval.Also send a notification about the expiration of login.However in some cases, it might increase the CPU usage and slow down the website.
Today we will learn more about WordPress Heartbeat API.You might have a question in mind about this strange name, Let’s understand that too.
What is Heartbeat API?
Heartbeat API name itself introduce us, mainly it is kind of pulse effect which processed to perform in some frequency.
Basically, it simply send AJAX requests automatically after a certain time(may be intervals of 15-30 seconds) to the server and trigger some events after receiving a response from the server. This action is performed very quickly and sometimes it crash the browser, but it helps to prevent the data loss by simultaneously editing of the post.
Disable HeartBeat API is recommended for your WordPress site if, you are facing resources issue.In some cases, The specific file admin-ajax.php call the WordPress heartbeat request and waits for an response from the server,sometimes responsible for increasing consumption of memory and CPU on the server due to frequent database calls compared to other requests which are not cached and slowed down your admin panel. At that time, It is necessary to Disable HeartBeat API.
NOTE: Once you disable HeartBeat API, It will turn off the autosave feature of WordPress. We have previously written about POST AUTOSAVE INTERVAL IN WORDPRESS.
How to Disable HeartBeat API?
To Disable HeartBeat API, You just need to write following line of code inside the active theme’s functions.php file.
1 2 3 4 5 6 7 8 9 | add_action( 'admin_enqueue_scripts', 'disable_wp_heartbeat' ); global $pagenow; if ( $pagenow != 'post.php' && $pagenow != 'post-new.php' && $pagenow != 'edit.php' ){ wp_deregister_script('heartbeat'); } } |
Above line of code will disable HeartBeat API for the site except post.php,post-new.php and edit.php.This is the best way by adding few lines of code into WordPress file rather than installing the plug-in which might affect a site on upgrade.
How To Change the HeartBeat API frequency?
If you don’t want to disable HeartBeat API, You can change the frequency of it which slow the heartbeat call.Although, You can set the minimum 5 and maximum 60 seconds.Here is the example to set the time interval in which each pulse is executed.
Let’s take a look at how to limit WordPress Heartbeat API.
1 2 3 4 5 6 7 8 9 | function heartbeat_frequency( $settings ) { global $heartbeat_frequency; $settings['interval'] = 40; return $settings; } |
In the case of any unexpected query or issue, simply write in comment section.
Comments (1)