Heartbeat API

What is Heartbeat API

The Heartbeat API in WordPress acts like your website’s pulse: it sends a tiny “I’m alive” ping from your browser to your server every few seconds. This automatic request/response cycle syncs your site’s data, posts, and dashboard, providing real-time updates and crucial functionality for editors, plugins, and admins.

In this glossary article, you’ll learn exactly how the WordPress Heartbeat API works, why it matters, and how to monitor, control, or disable it if it creates high CPU demand on your hosting.

Why the WordPress Heartbeat API Matters?

The Heartbeat API underpins many of WordPress’s most user-friendly features by keeping your browser and server in constant communication. Without it, you would lose convenient auto-saves, collaborative post locks, and live dashboard updates.

How Heartbeat API Works: The Basics

Heartbeat operates via small AJAX calls to your server, letting WordPress coordinate data without complete page refreshes. These frequent pings optimize functionality and security by ensuring editors see up-to-date information and that sessions remain valid.

js

// Example: Listen for the next heartbeat tick in the frontend
jQuery(document).on(‘heartbeat-tick’, function(event, data) {
console.log(‘Heartbeat response received:’, data);
});

Core Hooks and Code Examples

Developers leverage Heartbeat’s built-in hooks to add custom features or adjust its behavior. You can tailor the API to exchange the needed data by tapping into these JavaScript and PHP events.

JavaScript Hooks

Below are two primary JS hooks you can tie into for front-end control:

js

// Attach extra info on every heartbeat-send event
jQuery(document).on(‘heartbeat-send’, function(e, data) {
data.customValue = jQuery(‘#my-field’).val();
});

PHP Hooks

On the server side, these hooks allow you to modify or react to Heartbeat calls:

php

add_filter(‘heartbeat_send’, ‘my_heartbeat_send’, 10, 2);
function my_heartbeat_send($response, $screen_id) {
if ($screen_id === ‘post’) {
$response[‘server_time’] = time();
}
return $response;
}

Common Use Cases of WordPress Heartbeat API

Heartbeat’s real-time capabilities power many everyday features in WordPress. Syncing server and browser ensures that dashboards and editors stay current without manual clicks or full reloads.

Performance Considerations and High CPU Usage

While indispensable for real-time updates, Heartbeat can become a heavy CPU consumer on busy sites. Each frequent AJAX request to admin-ajax.php uses processing time and memory, which may push shared-hosting accounts over their resource limits.

How to Monitor Heartbeat Traffic?

Observing heartbeat activity can help you diagnose performance bottlenecks early. Use server logs and client-side tools to track request frequency and response times.

Controlling and Limiting the Heartbeat API

If Heartbeat’s default rhythm overwhelms your server, you can slow or disable it selectively. This lets you balance real-time features with optimal performance and resource control.

php

// Reduce Heartbeat frequency to once every 60 seconds
add_filter(‘heartbeat_settings’, function($settings) {
$settings[‘interval’] = 60;
return $settings;
});
// Disable on the front end entirely
add_action(‘init’, function() {
if (!is_admin()) wp_deregister_script(‘heartbeat’);
}, 1);

Risks of Disabling the Heartbeat API

Shutting off Heartbeat may ease CPU strain, but it comes at the cost of several key features. Before you disable it site-wide, understand the trade-offs in user experience and security.

Best Practices and Troubleshooting

Rather than kill the Heartbeat entirely, it’s wiser to fine-tune its behavior. By selectively throttling or logging Heartbeat, you preserve its benefits while maintaining server health.

Final Thought

The WordPress Heartbeat API keeps your site alive and in sync. It auto-saves drafts, shows real-time data, and prevents editing conflicts. Too many requests can raise CPU use. You can slow it down or disable it where needed. Test changes in a staging site first. With the right balance, Heartbeat boosts functionality without hurting performance.

For more WordPress terms and in-depth explanations, explore the Seahawk Media Glossary.