How to Disable or Limit WordPress Heartbeat API Effectively
Explanation
The Heartbeat API in WordPress is like a background messenger that keeps things updated in real-time, but it can sometimes be a bit too chatty, causing your site to slow down. Here's how you can manage it:
- Disable Heartbeat on Specific Pages: This code stops the Heartbeat API from running on the front-end (what visitors see) and on post edit pages (where you write posts). This can help reduce unnecessary server load.
- Limit Heartbeat Frequency: By setting the Heartbeat to run every 60 seconds instead of more frequently, you can ease the strain on your server. This means it checks in less often, which can help improve performance.
These tweaks are handy if you're noticing performance issues or high CPU usage linked to the Heartbeat API. Adjusting these settings can make your site run smoother without losing essential functionality.
Code
<?php
/**
* Disable or limit WordPress Heartbeat API to improve performance.
*
* This snippet allows you to either completely disable the Heartbeat API
* or limit its frequency to reduce server load and improve site performance.
*
* @package WordPress
*/
/**
* Disable Heartbeat API on specific pages or globally.
*
* @param array $locations The Heartbeat API locations.
* @return array Modified Heartbeat API locations.
*/
function wp_dudecom_disable_heartbeat( $locations ) {
// Disable Heartbeat API on the front-end and post edit pages.
unset( $locations['post-edit'] );
unset( $locations['front'] );
return $locations;
}
add_filter( 'heartbeat_locations', 'wp_dudecom_disable_heartbeat' );
/**
* Limit Heartbeat API frequency.
*
* @param int $settings The Heartbeat API settings.
* @return int Modified Heartbeat API settings.
*/
function wp_dudecom_limit_heartbeat_frequency( $settings ) {
// Limit Heartbeat API to run every 60 seconds.
$settings['interval'] = 60;
return $settings;
}
add_filter( 'heartbeat_settings', 'wp_dudecom_limit_heartbeat_frequency' );
?>
Instructions
To implement the code that disables or limits the WordPress Heartbeat API, follow these steps:
File Location: You will need to add the code to your theme's functions.php
file or a custom plugin file.
Prerequisites: Ensure you have access to your WordPress site's files, either via a file manager in your hosting control panel or through an FTP client.
Implementation Steps:
- Access Your WordPress Files:
- Log in to your hosting account and navigate to the file manager, or use an FTP client to connect to your website.
- Locate the
functions.php
File:- Navigate to
wp-content/themes/your-active-theme/
. - Open the
functions.php
file for editing.
- Navigate to
- Add the Code:
- Copy the provided code snippet.
- Paste it at the end of the
functions.php
file.
- Save Changes:
- Save the changes to the
functions.php
file.
- Save the changes to the
- Test Your Site:
- Visit your website to ensure everything is functioning correctly.
- Check the performance to see if there is an improvement.
If you need further assistance with this implementation or require more advanced functionality, consider reaching out to the experts at wp-dude.com for professional help.