wp_ajax_heartbeat

Ajax handler for the Heartbeat API.

Description

wp_ajax_heartbeat(); 

Runs when the user is logged in.


Usage

  1. if ( !function_exists( 'wp_ajax_heartbeat' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_heartbeat(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_heartbeat() { 
  2. if ( empty( $_POST['_nonce'] ) ) { 
  3.  
  4. $response = $data = array(); 
  5. $nonce_state = wp_verify_nonce( $_POST['_nonce'], 'heartbeat-nonce' ); 
  6.  
  7. // screen_id is the same as $current_screen->id and the JS global 'pagenow'. 
  8. if ( ! empty( $_POST['screen_id'] ) ) { 
  9. $screen_id = sanitize_key($_POST['screen_id']); 
  10. } else { 
  11. $screen_id = 'front'; 
  12.  
  13. if ( ! empty( $_POST['data'] ) ) { 
  14. $data = wp_unslash( (array) $_POST['data'] ); 
  15.  
  16. if ( 1 !== $nonce_state ) { 
  17. $response = apply_filters( 'wp_refresh_nonces', $response, $data, $screen_id ); 
  18.  
  19. if ( false === $nonce_state ) { 
  20. // User is logged in but nonces have expired. 
  21. $response['nonces_expired'] = true; 
  22. wp_send_json( $response ); 
  23.  
  24. if ( ! empty( $data ) ) { 
  25. /** 
  26. * Filters the Heartbeat response received. 
  27. * @since 3.6.0 
  28. * @param array $response The Heartbeat response. 
  29. * @param array $data The $_POST data sent. 
  30. * @param string $screen_id The screen id. 
  31. */ 
  32. $response = apply_filters( 'heartbeat_received', $response, $data, $screen_id ); 
  33.  
  34. /** 
  35. * Filters the Heartbeat response sent. 
  36. * @since 3.6.0 
  37. * @param array $response The Heartbeat response. 
  38. * @param string $screen_id The screen id. 
  39. */ 
  40. $response = apply_filters( 'heartbeat_send', $response, $screen_id ); 
  41.  
  42. /** 
  43. * Fires when Heartbeat ticks in logged-in environments. 
  44. * Allows the transport to be easily replaced with long-polling. 
  45. * @since 3.6.0 
  46. * @param array $response The Heartbeat response. 
  47. * @param string $screen_id The screen id. 
  48. */ 
  49. do_action( 'heartbeat_tick', $response, $screen_id ); 
  50.  
  51. // Send the current time according to the server 
  52. $response['server_time'] = time(); 
  53.  
  54. wp_send_json( $response );