Title: wp_ajax_heartbeat
Published: April 25, 2014
Last modified: February 24, 2026

---

# wp_ajax_heartbeat()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#wp--skip-link--target)

Handles the Heartbeat API via AJAX.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#description)󠁿

Runs when the user is logged in.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#source)󠁿

    ```php
    function wp_ajax_heartbeat() {
    	if ( empty( $_POST['_nonce'] ) ) {
    		wp_send_json_error();
    	}

    	$response    = array();
    	$data        = array();
    	$nonce_state = wp_verify_nonce( $_POST['_nonce'], 'heartbeat-nonce' );

    	// 'screen_id' is the same as $current_screen->id and the JS global 'pagenow'.
    	if ( ! empty( $_POST['screen_id'] ) ) {
    		$screen_id = sanitize_key( $_POST['screen_id'] );
    	} else {
    		$screen_id = 'front';
    	}

    	if ( ! empty( $_POST['data'] ) ) {
    		$data = wp_unslash( (array) $_POST['data'] );
    	}

    	if ( 1 !== $nonce_state ) {
    		/**
    		 * Filters the nonces to send to the New/Edit Post screen.
    		 *
    		 * @since 4.3.0
    		 *
    		 * @param array  $response  The Heartbeat response.
    		 * @param array  $data      The $_POST data sent.
    		 * @param string $screen_id The screen ID.
    		 */
    		$response = apply_filters( 'wp_refresh_nonces', $response, $data, $screen_id );

    		if ( false === $nonce_state ) {
    			// User is logged in but nonces have expired.
    			$response['nonces_expired'] = true;
    			wp_send_json( $response );
    		}
    	}

    	if ( ! empty( $data ) ) {
    		/**
    		 * Filters the Heartbeat response received.
    		 *
    		 * @since 3.6.0
    		 *
    		 * @param array  $response  The Heartbeat response.
    		 * @param array  $data      The $_POST data sent.
    		 * @param string $screen_id The screen ID.
    		 */
    		$response = apply_filters( 'heartbeat_received', $response, $data, $screen_id );
    	}

    	/**
    	 * Filters the Heartbeat response sent.
    	 *
    	 * @since 3.6.0
    	 *
    	 * @param array  $response  The Heartbeat response.
    	 * @param string $screen_id The screen ID.
    	 */
    	$response = apply_filters( 'heartbeat_send', $response, $screen_id );

    	/**
    	 * Fires when Heartbeat ticks in logged-in environments.
    	 *
    	 * Allows the transport to be easily replaced with long-polling.
    	 *
    	 * @since 3.6.0
    	 *
    	 * @param array  $response  The Heartbeat response.
    	 * @param string $screen_id The screen ID.
    	 */
    	do_action( 'heartbeat_tick', $response, $screen_id );

    	// Send the current time according to the server.
    	$response['server_time'] = time();

    	wp_send_json( $response );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/ajax-actions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/ajax-actions.php#L3457)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ajax-actions.php#L3457-L3535)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#hooks)󠁿

 [apply_filters( ‘heartbeat_received’, array $response, array $data, string $screen_id )](https://developer.wordpress.org/reference/hooks/heartbeat_received/)

Filters the Heartbeat response received.

 [apply_filters( ‘heartbeat_send’, array $response, string $screen_id )](https://developer.wordpress.org/reference/hooks/heartbeat_send/)

Filters the Heartbeat response sent.

 [do_action( ‘heartbeat_tick’, array $response, string $screen_id )](https://developer.wordpress.org/reference/hooks/heartbeat_tick/)

Fires when Heartbeat ticks in logged-in environments.

 [apply_filters( ‘wp_refresh_nonces’, array $response, array $data, string $screen_id )](https://developer.wordpress.org/reference/hooks/wp_refresh_nonces/)

Filters the nonces to send to the New/Edit Post screen.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_verify_nonce()](https://developer.wordpress.org/reference/functions/wp_verify_nonce/)`wp-includes/pluggable.php` |

Verifies that a correct security nonce was used with time limit.

  | 
| [wp_send_json()](https://developer.wordpress.org/reference/functions/wp_send_json/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request.

  | 
| [wp_unslash()](https://developer.wordpress.org/reference/functions/wp_unslash/)`wp-includes/formatting.php` |

Removes slashes from a string or recursively removes slashes from strings within an array.

  | 
| [sanitize_key()](https://developer.wordpress.org/reference/functions/sanitize_key/)`wp-includes/formatting.php` |

Sanitizes a string key.

  | 
| [wp_send_json_error()](https://developer.wordpress.org/reference/functions/wp_send_json_error/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating failure.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [do_action()](https://developer.wordpress.org/reference/functions/do_action/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to an action hook.

  |

[Show 5 more](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_heartbeat/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.6.0](https://developer.wordpress.org/reference/since/3.6.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_ajax_heartbeat%2F)
before being able to contribute a note or feedback.