Using WordPress ‘heartbeat_settings’ PHP filter

The heartbeat_settings WordPress PHP Filter allows you to modify the Heartbeat settings in WordPress.

Usage

add_filter('heartbeat_settings', 'your_custom_function');
function your_custom_function($settings) {
    // your custom code here
    return $settings;
}

Parameters

  • $settings (array) – Heartbeat settings array.

More information

See WordPress Developer Resources: heartbeat_settings

Examples

Change Heartbeat frequency

This example adjusts the Heartbeat interval to 45 seconds.

add_filter('heartbeat_settings', 'change_heartbeat_frequency');
function change_heartbeat_frequency($settings) {
    $settings['interval'] = 45; // Set interval to 45 seconds
    return $settings;
}

Disable Heartbeat

This example completely disables the Heartbeat.

add_filter('heartbeat_settings', 'disable_heartbeat');
function disable_heartbeat($settings) {
    $settings['disabled'] = true; // Disable Heartbeat
    return $settings;
}

Enable Heartbeat only on post edit pages

This example enables the Heartbeat only on post edit pages.

add_filter('heartbeat_settings', 'enable_heartbeat_on_post_edit_pages');
function enable_heartbeat_on_post_edit_pages($settings) {
    if (get_current_screen()->base !== 'post') {
        $settings['disabled'] = true; // Disable Heartbeat
    }
    return $settings;
}

Change Heartbeat frequency based on user role

This example changes the Heartbeat frequency based on the user’s role.

add_filter('heartbeat_settings', 'change_heartbeat_frequency_based_on_role');
function change_heartbeat_frequency_based_on_role($settings) {
    $user = wp_get_current_user();
    if (in_array('administrator', $user->roles)) {
        $settings['interval'] = 15; // Set interval to 15 seconds for administrators
    } else {
        $settings['interval'] = 60; // Set interval to 60 seconds for other users
    }
    return $settings;
}

Modify Heartbeat settings on the frontend

This example modifies the Heartbeat settings only on the frontend of the website.

add_filter('heartbeat_settings', 'modify_heartbeat_on_frontend');
function modify_heartbeat_on_frontend($settings) {
    if (!is_admin()) {
        $settings['interval'] = 30; // Set interval to 30 seconds on frontend
        $settings['screen_id'][] = 'frontend'; // Add a new screen_id for the frontend
    }
    return $settings;
}