wp_ajax_nopriv_heartbeat

Ajax handler for the Heartbeat API in the no-privilege context.

Description

wp_ajax_nopriv_heartbeat(); 

Runs when the user is not logged in.


Usage

  1. if ( !function_exists( 'wp_ajax_nopriv_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_nopriv_heartbeat(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_nopriv_heartbeat() { 
  2. $response = array(); 
  3.  
  4. // screen_id is the same as $current_screen->id and the JS global 'pagenow'. 
  5. if ( ! empty($_POST['screen_id']) ) 
  6. $screen_id = sanitize_key($_POST['screen_id']); 
  7. else 
  8. $screen_id = 'front'; 
  9.  
  10. if ( ! empty($_POST['data']) ) { 
  11. $data = wp_unslash( (array) $_POST['data'] ); 
  12.  
  13. /** 
  14. * Filters Heartbeat Ajax response in no-privilege environments. 
  15. * @since 3.6.0 
  16. * @param array|object $response The no-priv Heartbeat response object or array. 
  17. * @param array $data An array of data passed via $_POST. 
  18. * @param string $screen_id The screen id. 
  19. */ 
  20. $response = apply_filters( 'heartbeat_nopriv_received', $response, $data, $screen_id ); 
  21.  
  22. /** 
  23. * Filters Heartbeat Ajax response when no data is passed. 
  24. * @since 3.6.0 
  25. * @param array|object $response The Heartbeat response object or array. 
  26. * @param string $screen_id The screen id. 
  27. */ 
  28. $response = apply_filters( 'heartbeat_nopriv_send', $response, $screen_id ); 
  29.  
  30. /** 
  31. * Fires when Heartbeat ticks in no-privilege environments. 
  32. * Allows the transport to be easily replaced with long-polling. 
  33. * @since 3.6.0 
  34. * @param array|object $response The no-priv Heartbeat response. 
  35. * @param string $screen_id The screen id. 
  36. */ 
  37. do_action( 'heartbeat_nopriv_tick', $response, $screen_id ); 
  38.  
  39. // Send the current time according to the server. 
  40. $response['server_time'] = time(); 
  41.  
  42. wp_send_json($response);