wpcf7_ajax_json_echo

The Contact Form 7 wpcf7 ajax json echo function.

Description

wpcf7_ajax_json_echo(); 

Usage

  1. if ( !function_exists( 'wpcf7_ajax_json_echo' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'contact-form-7/includes/controller.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wpcf7_ajax_json_echo(); 
  7.  

Defined (1)

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

/includes/controller.php  
  1. function wpcf7_ajax_json_echo() { 
  2. $echo = ''; 
  3.  
  4. if ( isset( $_POST['_wpcf7'] ) ) { 
  5. $id = (int) $_POST['_wpcf7']; 
  6. $unit_tag = wpcf7_sanitize_unit_tag( $_POST['_wpcf7_unit_tag'] ); 
  7.  
  8. if ( $contact_form = wpcf7_contact_form( $id ) ) { 
  9. $items = array( 
  10. 'mailSent' => false,  
  11. 'into' => '#' . $unit_tag,  
  12. 'captcha' => null,  
  13. ); 
  14.  
  15. $result = $contact_form->submit( true ); 
  16.  
  17. if ( ! empty( $result['message'] ) ) { 
  18. $items['message'] = $result['message']; 
  19.  
  20. if ( 'mail_sent' == $result['status'] ) { 
  21. $items['mailSent'] = true; 
  22.  
  23. if ( 'validation_failed' == $result['status'] ) { 
  24. $invalids = array(); 
  25.  
  26. foreach ( $result['invalid_fields'] as $name => $field ) { 
  27. $invalids[] = array( 
  28. 'into' => 'span.wpcf7-form-control-wrap.' 
  29. . sanitize_html_class( $name ),  
  30. 'message' => $field['reason'],  
  31. 'idref' => $field['idref'],  
  32. ); 
  33.  
  34. $items['invalids'] = $invalids; 
  35.  
  36. if ( 'spam' == $result['status'] ) { 
  37. $items['spam'] = true; 
  38.  
  39. if ( ! empty( $result['scripts_on_sent_ok'] ) ) { 
  40. $items['onSentOk'] = $result['scripts_on_sent_ok']; 
  41.  
  42. if ( ! empty( $result['scripts_on_submit'] ) ) { 
  43. $items['onSubmit'] = $result['scripts_on_submit']; 
  44.  
  45. $items = apply_filters( 'wpcf7_ajax_json_echo', $items, $result ); 
  46.  
  47. $echo = wp_json_encode( $items ); 
  48.  
  49. if ( wpcf7_is_xhr() ) { 
  50. @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); 
  51. echo $echo; 
  52. } else { 
  53. @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); 
  54. echo '<textarea>' . $echo . '</textarea>'; 
  55.  
  56. exit();