bbp_ajax_response

Helper method to return JSON response for the ajax calls.

Description

bbp_ajax_response( (constant) $success = false, (string) $content = '', (int) $status = -1, (array) $extras = array() ); 

Parameters (4)

0. $success — Optional. (constant) => false
The success.
1. $content — Optional. (string) => ''
The content.
2. $status — Optional. (int) => -1
The status.
3. $extras — Optional. (array) => array()
The extras.

Usage

  1. if ( !function_exists( 'bbp_ajax_response' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/common/ajax.php'; 
  3.  
  4. // The success. 
  5. $success = false; 
  6.  
  7. // The content. 
  8. $content = ''; 
  9.  
  10. // The status. 
  11. $status = -1; 
  12.  
  13. // The extras. 
  14. $extras = array(); 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = bbp_ajax_response($success, $content, $status, $extras); 
  18.  

Defined (1)

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

/includes/common/ajax.php  
  1. function bbp_ajax_response( $success = false, $content = '', $status = -1, $extras = array() ) { 
  2.  
  3. // Set status to 200 if setting response as successful 
  4. if ( ( true === $success ) && ( -1 === $status ) ) 
  5. $status = 200; 
  6.  
  7. // Setup the response array 
  8. $response = array( 
  9. 'success' => $success,  
  10. 'status' => $status,  
  11. 'content' => $content 
  12. ); 
  13.  
  14. // Merge extra response parameters in 
  15. if ( !empty( $extras ) && is_array( $extras ) ) { 
  16. $response = array_merge( $response, $extras ); 
  17.  
  18. // Send back the JSON 
  19. @header( 'Content-type: application/json' ); 
  20. echo json_encode( $response ); 
  21. die();