wp_ajax_heartbeat

The timeline below displays how wordpress function wp_ajax_heartbeat has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Handles the Heartbeat API via AJAX.
 *
 * Runs when the user is logged in.
 *
 * @since 3.6.0
 */
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);
}

WordPress Version: 5.5

/**
 * Ajax handler for the Heartbeat API.
 *
 * Runs when the user is logged in.
 *
 * @since 3.6.0
 */
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);
}

WordPress Version: 5.4

/**
 * Ajax handler for the Heartbeat API.
 *
 * Runs when the user is logged in.
 *
 * @since 3.6.0
 */
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);
}

WordPress Version: 5.3

/**
 * Ajax handler for the Heartbeat API.
 *
 * Runs when the user is logged in.
 *
 * @since 3.6.0
 */
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);
}

WordPress Version: 5.1

/**
 * Ajax handler for the Heartbeat API.
 *
 * Runs when the user is logged in.
 *
 * @since 3.6.0
 */
function wp_ajax_heartbeat()
{
    if (empty($_POST['_nonce'])) {
        wp_send_json_error();
    }
    $response = $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);
}

WordPress Version: 4.6

/**
 * Ajax handler for the Heartbeat API.
 *
 * Runs when the user is logged in.
 *
 * @since 3.6.0
 */
function wp_ajax_heartbeat()
{
    if (empty($_POST['_nonce'])) {
        wp_send_json_error();
    }
    $response = $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) {
        $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);
}

WordPress Version: 4.5

/**
 * Ajax handler for the Heartbeat API.
 *
 * Runs when the user is logged in.
 *
 * @since 3.6.0
 */
function wp_ajax_heartbeat()
{
    if (empty($_POST['_nonce'])) {
        wp_send_json_error();
    }
    $response = $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) {
        $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)) {
        /**
         * Filter 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);
    }
    /**
     * Filter 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);
}

WordPress Version: 4.3

/**
 * Ajax handler for the Heartbeat API.
 *
 * Runs when the user is logged in.
 *
 * @since 3.6.0
 */
function wp_ajax_heartbeat()
{
    if (empty($_POST['_nonce'])) {
        wp_send_json_error();
    }
    $response = $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) {
        $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)) {
        /**
         * Filter the Heartbeat response received.
         *
         * @since 3.6.0
         *
         * @param array|object $response  The Heartbeat response object or array.
         * @param array        $data      The $_POST data sent.
         * @param string       $screen_id The screen id.
         */
        $response = apply_filters('heartbeat_received', $response, $data, $screen_id);
    }
    /**
     * Filter the Heartbeat response sent.
     *
     * @since 3.6.0
     *
     * @param array|object $response  The Heartbeat response object or array.
     * @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|object $response  The Heartbeat response object or array.
     * @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);
}

WordPress Version: 4.0

/**
 * Ajax handler for the Heartbeat API.
 *
 * Runs when the user is logged in.
 *
 * @since 3.6.0
 */
function wp_ajax_heartbeat()
{
    if (empty($_POST['_nonce'])) {
        wp_send_json_error();
    }
    $response = array();
    if (false === wp_verify_nonce($_POST['_nonce'], 'heartbeat-nonce')) {
        // User is logged in but nonces have expired.
        $response['nonces_expired'] = true;
        wp_send_json($response);
    }
    // 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']);
        /**
         * Filter the Heartbeat response received.
         *
         * @since 3.6.0
         *
         * @param array|object $response  The Heartbeat response object or array.
         * @param array        $data      The $_POST data sent.
         * @param string       $screen_id The screen id.
         */
        $response = apply_filters('heartbeat_received', $response, $data, $screen_id);
    }
    /**
     * Filter the Heartbeat response sent.
     *
     * @since 3.6.0
     *
     * @param array|object $response  The Heartbeat response object or array.
     * @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|object $response  The Heartbeat response object or array.
     * @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);
}

WordPress Version: 3.9

/**
 * Heartbeat API (experimental)
 *
 * Runs when the user is logged in.
 */
function wp_ajax_heartbeat()
{
    if (empty($_POST['_nonce'])) {
        wp_send_json_error();
    }
    $response = array();
    if (false === wp_verify_nonce($_POST['_nonce'], 'heartbeat-nonce')) {
        // User is logged in but nonces have expired.
        $response['nonces_expired'] = true;
        wp_send_json($response);
    }
    // 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']);
        /**
         * Filter the Heartbeat response received.
         *
         * @since 3.6.0
         *
         * @param array|object $response  The Heartbeat response object or array.
         * @param array        $data      The $_POST data sent.
         * @param string       $screen_id The screen id.
         */
        $response = apply_filters('heartbeat_received', $response, $data, $screen_id);
    }
    /**
     * Filter the Heartbeat response sent.
     *
     * @since 3.6.0
     *
     * @param array|object $response  The Heartbeat response object or array.
     * @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|object $response  The Heartbeat response object or array.
     * @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);
}

WordPress Version: 3.7

/**
 * Heartbeat API (experimental)
 *
 * Runs when the user is logged in.
 */
function wp_ajax_heartbeat()
{
    if (empty($_POST['_nonce'])) {
        wp_send_json_error();
    }
    $response = array();
    if (false === wp_verify_nonce($_POST['_nonce'], 'heartbeat-nonce')) {
        // User is logged in but nonces have expired.
        $response['nonces_expired'] = true;
        wp_send_json($response);
    }
    // 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 = (array) $_POST['data'];
        /**
         * Filter the Heartbeat response received.
         *
         * @since 3.6.0
         *
         * @param array|object $response  The Heartbeat response object or array.
         * @param array        $data      The $_POST data sent.
         * @param string       $screen_id The screen id.
         */
        $response = apply_filters('heartbeat_received', $response, $data, $screen_id);
    }
    /**
     * Filter the Heartbeat response sent.
     *
     * @since 3.6.0
     *
     * @param array|object $response  The Heartbeat response object or array.
     * @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|object $response  The Heartbeat response object or array.
     * @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);
}