The WordPress Heartbeat API is responsible for sending periodic requests between the browser and the server to check for updates, such as post autosaves and plugin updates. While it can be useful, it can also consume server resources, especially if you have many users or if some users are constantly editing posts.
To disable the WordPress Heartbeat API for specific users, you can use the wp_authenticate
hook along with the wp_get_current_user()
function to check if the current user should have the Heartbeat API disabled. If so, you can use the wp_deregister_script()
function to remove the heartbeat script.
Here’s an example code snippet that demonstrates how to disable the WordPress Heartbeat API for specific users:
function wpsnippets_disable_heartbeat_for_specific_users() {
// Get the current user object
$current_user = wp_get_current_user();
// Check if the user should have the Heartbeat API disabled
if ( $current_user && $current_user->user_login === 'username' ) {
// Deregister the heartbeat script
wp_deregister_script( 'heartbeat' );
}
}
add_action( 'wp_authenticate', 'wpsnippets_disable_heartbeat_for_specific_users' );
In this example, the code checks if the current user’s login username is 'username'
. If it matches, the Heartbeat API script is deregistered, effectively disabling it for that specific user.
You can modify the 'username'
condition to match the specific user(s) you want to disable the Heartbeat API for. You can also add additional conditions or use different user properties to determine which users should have the Heartbeat API disabled.
This code snippet can be useful in scenarios where you want to reduce server resource usage for specific users who don’t require real-time updates or have a high frequency of editing posts.
Examples
Example #1: Disable WordPress Heartbeat for Specific User Roles
This use case demonstrates how to disable the WordPress Heartbeat API for specific user roles using a custom PHP function. The code example below disables the Heartbeat API for users with the “editor” role.
function wpsnippets_disable_heartbeat_for_editors() {
if ( current_user_can( 'editor' ) ) {
wp_deregister_script( 'heartbeat' );
}
}
add_action( 'init', 'wpsnippets_disable_heartbeat_for_editors', 1 );
Explanation: The wpsnippets_disable_heartbeat_for_editors
function checks if the current user has the “editor” role using the current_user_can
function. If the user has the “editor” role, the wp_deregister_script
function is called to deregister the Heartbeat API script.
Example #2: Disable WordPress Heartbeat for Specific User IDs
This use case demonstrates how to disable the WordPress Heartbeat API for specific user IDs using a custom PHP function. The code example below disables the Heartbeat API for users with the IDs 2 and 5.
function wpsnippets_disable_heartbeat_for_specific_users() {
$user_ids = array( 2, 5 );
if ( in_array( get_current_user_id(), $user_ids ) ) {
wp_deregister_script( 'heartbeat' );
}
}
add_action( 'init', 'wpsnippets_disable_heartbeat_for_specific_users', 1 );
Explanation: The wpsnippets_disable_heartbeat_for_specific_users
function checks if the current user’s ID is in the array of specified user IDs using the in_array
function. If the user’s ID is found in the array, the wp_deregister_script
function is called to deregister the Heartbeat API script.
Example #3: Disable WordPress Heartbeat for Specific Usernames
This use case demonstrates how to disable the WordPress Heartbeat API for specific usernames using a custom PHP function. The code example below disables the Heartbeat API for users with the usernames “user1” and “user2”.
function wpsnippets_disable_heartbeat_for_specific_usernames() {
$usernames = array( 'user1', 'user2' );
if ( in_array( wp_get_current_user()->user_login, $usernames ) ) {
wp_deregister_script( 'heartbeat' );
}
}
add_action( 'init', 'wpsnippets_disable_heartbeat_for_specific_usernames', 1 );
Explanation: The wpsnippets_disable_heartbeat_for_specific_usernames
function checks if the current user’s username is in the array of specified usernames using the in_array
function. If the user’s username is found in the array, the wp_deregister_script
function is called to deregister the Heartbeat API script.