'; // Get the contents of our custom field 'iwacontact_data' $iwacontact_data = $fields['iwacontact_data'][0]; // Find each field by splitting the custom field contents on ';;' $form_fields = preg_split( '/\;\;/', $iwacontact_data ); // Create a new array for the correctly ordered form fields $ordered_fields = array(); // For each form field foreach ( $form_fields as $field ) { // Find the field attributes by splitting the string on '::' $field_attrs = preg_split( '/\:\:/', $field ); // Add them to our ordered fields array array_push( $ordered_fields, $field_attrs ); } // Sort the ordered fields array by the specified display order usort( $ordered_fields, "sort_fields" ); // Set the $has_error variable to false $has_error = false; // For each field in our correctly ordered form field array foreach ( $ordered_fields as $field ) { // Place the field attributes in an easier to use associative array $field_vars = array( 'element_id' => $field[0], 'displayorder' => $field[1], 'fieldtype' => $field[2], 'fieldname' => $field[3], 'fieldoptions' => acontact_parse_special( $field[4] ), 'fieldrequired' => $field[5], 'fieldvalidation' => $field[6] ); $show_label = true; if ( key_exists( 7, $field ) ) { $field_vars = array_merge( $field_vars, array( 'fieldlabel' => $field[7] ) ); $show_label = ( $field_vars['fieldlabel'] != '1' ) ? false : true; } // Check if this was a graceful JavaScript failure $submitted_value = null; $field_error = null; // If this field was in the $_POST data if ( key_exists( $field_vars['element_id'], $_POST ) && key_exists( 'iwac_form_id', $_POST ) && $_POST['iwac_form_id'] == $post_id ) { // Set the submitted value for this field // If it was an array (ie multiple select box), make it a string $submitted_value = ( is_array( $_POST[$field_vars['element_id']] ) ) ? join( ',', $_POST[$field_vars['element_id']] ) : $_POST[$field_vars['element_id']]; // Check for empty required fields if ( $field_vars['fieldrequired'] == '1' && empty( $submitted_value ) ) { $field_error = 'This field is required'; $error = true; } // Or non-valid email addresses elseif ( $field_vars['fieldvalidation'] == 'email' && !preg_match( '/^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$/i', $submitted_value ) ) { $field_error = 'Please enter a valid email address'; $error = true; } } // Reset some variables $multiselect = ''; $input_type = 'text'; $field_id = $field_vars['element_id']; // Do a switch on the field type switch ( $field_vars['fieldtype'] ) { // If it's a password case 'password' : $input_type = 'password'; //break; // no break will continue to next case statement // If it's an input case 'input' : // Prepare the class names for this field $class = 'input '; $class .= ( $field_vars['fieldrequired'] == '1' ) ? 'required-field ' : ''; $class .= ( $field_vars['fieldvalidation'] != 'none' ) ? 'validate-' . $field_vars['fieldvalidation'] : ''; $class = rtrim( $class ); $default_value = ( $submitted_value != null ) ? $submitted_value : $field_vars['fieldoptions']; // Append the html for this field to the form html $form .= '
  1. ' . ( ( $show_label ) ? '' : '' ) . '' . ( ( $field_error != null ) ? $field_error : '' ) . '
  2. '; break; // If it's a text area case 'textarea' : $class = ''; $class .= ( $field_vars['fieldrequired'] == '1' ) ? 'required-field ' : ''; $class .= ( $field_vars['fieldvalidation'] != '' ) ? 'validate-' . $field_vars['fieldvalidation'] : ''; $class = rtrim( $class ); $default_value = ( $submitted_value != null ) ? $submitted_value : $field_vars['fieldoptions']; // Append the html for this field to the form html $form .= '
  3. ' . ( ( $show_label ) ? '' : '' ) . '' . ( ( $field_error != null ) ? $field_error : '' ) . '
  4. '; break; // If it's a check box case 'checkbox' : $class = ''; $class .= ( $field_vars['fieldrequired'] == '1' ) ? 'required-field ' : ''; $class = rtrim( $class ); $checked = ( $field_vars['fieldoptions'] == 'true' || $submitted_value == 'true' ) ? 'checked' : ''; // Append the html for this field to the form html $form .= '
  5. ' . '' . ( ( $show_label ) ? '' : '' ) . ( ( $field_error != null ) ? $field_error : '' ) . '
  6. '; break; // If it's a send copy check box case 'sendcopy' : $checked = ( $field_vars['fieldoptions'] == 'true' || $submitted_value == 'true' ) ? 'checked' : ''; // Append the html for this field to the form html $form .= '
  7. ' . '' . ( ( $show_label ) ? '' : '' ) . '
  8. '; break; // If it's a multi-select box case 'multiselect' : $multiselect = ' multiple="multiple" size="4" '; $field_id = $field_id . '[]'; $is_multiselect = 1; //break; // no break will also do next case statement // If it's a select box case 'selectbox' : // Prepare the class names for this field $class = ''; $class .= ( $field_vars['fieldrequired'] == '1' ) ? 'required-field ' : ''; $class = rtrim( $class ); // Append the html for this field to the form html $form .= '
  9. ' . ( ( $show_label ) ? '' : '' ) . '' . ( ( $field_error != null ) ? $field_error : '' ) . '
  10. '; break; // If it's a radio button set case 'radio' : // Prepare the class names for this field $class = ''; $class = rtrim( $class ); // Append the html for this field to the form html $form .= '
  11. ' . ( ( $show_label ) ? '' : '' ); $default_value = ( $submitted_value != null ) ? $submitted_value : ''; $options = preg_split( '/\n/', $field_vars['fieldoptions'] ); foreach ( $options as $option ) { $selected = ( trim( $option ) == trim( $submitted_value ) ) ? 'checked="checked"' : ''; $form .= " $option"; } $form .= ( ( $field_error != null ) ? $field_error : '' ) . '
  12. '; break; // If it's a hidden field case 'hidden' : // Prepare the class names for this field $default_value = ( $submitted_value != null ) ? $submitted_value : $field_vars['fieldoptions']; // Append the html for this field to the form html $form .= ''; break; // If it's a readonly input case 'readonly' : // Prepare the class names for this field $class = 'input read-only'; $default_value = ( $submitted_value != null ) ? $submitted_value : $field_vars['fieldoptions']; // Append the html for this field to the form html $form .= '
  13. ' . ( ( $show_label ) ? '' : '' ) . '' . ( ( $field_error != null ) ? $field_error : '' ) . '
  14. '; break; // If it's a heading 1 case 'h1' : // Prepare the class names for this field $class = ''; $class = rtrim( $class ); // Append the html for this field to the form html $form .= '
  15. ' . '

    ' . $field_vars['fieldname'] . '

    ' . '
  16. '; break; // If it's a heading 2 case 'h2' : // Prepare the class names for this field $class = ''; $class = rtrim( $class ); // Append the html for this field to the form html $form .= '
  17. ' . '

    ' . $field_vars['fieldname'] . '

    ' . '
  18. '; break; // If it's a heading 3 case 'h3' : // Prepare the class names for this field $class = ''; $class = rtrim( $class ); // Append the html for this field to the form html $form .= '
  19. ' . '

    ' . $field_vars['fieldname'] . '

    ' . '
  20. '; break; // If it's a heading 4 case 'h4' : // Prepare the class names for this field $class = ''; $class = rtrim( $class ); // Append the html for this field to the form html $form .= '
  21. ' . '

    ' . $field_vars['fieldname'] . '

    ' . '
  22. '; break; } } // Prepare some default values $ajax_result = ''; $ajax_display = ''; $submit_disabled = ''; // If this was a submission and there where no errors if ( key_exists( 'iwac_form_id', $_POST ) && $_POST['iwac_form_id'] == $post_id && !$error ) { $ajax_result = 'Your message has been sent successfully!'; $ajax_display = 'style="display: inline;"'; $submit_disabled = 'disabled="disabled"'; } // Add the submit button row html to the form html $form .= '
  23. ' . '' . '' . "$ajax_result" . "" . 'Loading ...' . '
  24. ' . '
'; // Return the form html return $form; } } /** * The call back function for our custom * short code [insert_ajaxcontact] * * @param array $atts The attributes **/ function insert_ajaxcontact( $atts ) { $the_ID = ( is_array( $atts ) && key_exists( 'id', $atts ) ) ? $atts['id'] : get_the_ID(); return get_contact_form( $the_ID ); } add_shortcode( 'insert_ajaxcontact', 'insert_ajaxcontact' ); /** * The AJAX handler function that catches AJAX * post submissions and processes them. This function * also prepares and sends the contact notification email. **/ function ajax_handler() { // If our hidden field 'iwac_submitted' exists in the $_POST array if ( key_exists( 'iwac_submitted', $_POST ) ) { // Set the blog name to $blog_name $blog_name = get_bloginfo( 'name' ); // Start the email body text $email_body = "There has been a new contact form submission at $blog_name"; // Get the custom fields array for this post $fields = get_post_custom( $_POST['iwac_form_id'] ); // Get the contents of our custom field 'iwacontact_data' $iwacontact_data = $fields['iwacontact_data'][0]; // Find each field by splitting the custom field contents on ';;' $form_fields = preg_split( '/\;\;/', $iwacontact_data ); // Create a new array for the correctly ordered form fields $ordered_fields = array(); // For each form field foreach ( $form_fields as $field ) { // Find the field attributes by splitting the string on '::' $field_attrs = preg_split( '/\:\:/', $field ); // Add them to our ordered fields array array_push( $ordered_fields, $field_attrs ); } // Sort the ordered fields array by the specified display order usort( $ordered_fields, "sort_fields" ); // Set the $has_error variable to false $has_error = false; // For each field in our correctly ordered form field array foreach ( $ordered_fields as $field ) { // Place the field attributes in an easier to use associative array $field_vars = array( 'element_id' => $field[0], 'displayorder' => $field[1], 'fieldtype' => $field[2], 'fieldname' => $field[3], 'fieldoptions' => $field[4], 'fieldrequired' => $field[5], 'fieldvalidation' => $field[6] ); // Set the submitted value for this field // If it was an array (ie multiple select box), make it a string $submitted_value = ( is_array( $_POST[$field_vars['element_id']] ) ) ? join( ',', $_POST[$field_vars['element_id']] ) : $_POST[$field_vars['element_id']]; // Check for empty required fields if ( $field_vars['fieldrequired'] == '1' && trim( $submitted_value ) == '' ) { $has_error = true; } // Or non-valid email addresses elseif ( $field_vars['fieldvalidation'] == 'email' && !preg_match( '/^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$/i', $submitted_value ) ) { $has_error = true; } // Otherwise, add the field name and it's submitted value to our email body variable else { $field_value = ( is_array( $_POST[ $field_vars['element_id'] ] ) ) ? implode( ', ', $_POST[ $field_vars['element_id'] ] ) : trim( $_POST[ $field_vars['element_id'] ] ); $email_body .= "\n\n" . $field_vars['fieldname'] . ":\n" . $field_value ; } } // If no errors occurred if ( !$has_error ) { // Set the subject, from and to address as the one specified in the post custom fields $from = $fields['iwacontact_from'][0]; $email_to = $fields['iwacontact_sendto'][0]; $subject = $fields['iwacontact_subject'][0]; // Set the email body as the variable we where adding content to before $body = $email_body; // Set the email from headers $headers = 'From: ' . get_bloginfo( 'name' ) . " <$from>" . "\r\n" . 'Reply-To: ' . $from; // Use wp_mail to send the message wp_mail( $email_to, $subject, $body, $headers ); // If there was a field named send_copy and it's value was true, send a copy to the form submitter if ( key_exists( 'send_copy', $_POST ) && $_POST['send_copy'] == 'true' ) { $subject = 'Your email to ' . get_bloginfo( 'name' ); $headers = 'From: ' . get_bloginfo( 'name' ) . " <$from>"; wp_mail( $email, $subject, $body, $headers ); } } } } add_action( 'init', 'ajax_handler' );