Last active
August 19, 2022 19:38
-
-
Save Mamikonars/898dc1775c6884f1a1def871d93095b0 to your computer and use it in GitHub Desktop.
WordPress ajax form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'wp_ajax_ajax_form_action', 'ajax_action_callback' ); | |
add_action( 'wp_ajax_nopriv_ajax_form_action', 'ajax_action_callback' ); | |
function ajax_action_callback() { | |
// errors array | |
$errors = []; | |
// checking nonce | |
if ( !wp_verify_nonce( $_POST['nonce'], 'ajax-form-nonce' ) ) { | |
wp_die( esc_html__('Something went wrong') ); | |
} | |
// checking spam | |
if ( $_POST['form_anticheck'] === false || !empty( $_POST['form_submitted'] ) ) { | |
wp_die( esc_html_e('Something went wrong') ); | |
} | |
// checking type | |
$data_type = $_POST['data_type'] ? sanitize_text_field( $_POST['data_type'] ) : ''; | |
// checking name field | |
if ( empty( $_POST['form_name'] ) || !isset( $_POST['form_name'] ) ) { | |
$errors['name'] = esc_html__('Please enter your name'); | |
} else { | |
$form_name = sanitize_text_field( $_POST['form_name'] ); | |
} | |
// checking mail field | |
if ( empty( $_POST['form_mail'] ) || !isset( $_POST['form_mail'] ) ) { | |
$errors['mail'] = esc_html__('Please enter your mail'); | |
} else { | |
$form_mail = sanitize_text_field( $_POST['form_mail'] ); | |
} | |
// if message is empty - sending default message | |
if ( empty( $_POST['form_message'] ) || !isset( $_POST['form_message'] ) ) { | |
$form_message = esc_html__('Empty message from site'); | |
} else { | |
$form_message = sanitize_text_field( $_POST['form_message'] ); | |
} | |
// checking errors array | |
if ( $errors ) { | |
wp_send_json_error( $errors ); | |
} else { | |
// checking from which site is mail | |
$home_url = wp_parse_url( home_url() ); | |
$subject = esc_html__('Mail from') . ' ' . $home_url['host']; | |
// mail headers | |
$email_to = '[email protected]'; | |
$email_from = get_option( 'admin_email' ); | |
// mail content | |
$body = esc_html__('Name: ') . $form_name . "<br>"; | |
$body .= esc_html__('Mail: ') . $form_mail . "<br>"; | |
$body .= esc_html__('Type: ') . $data_type . "<br>"; | |
$body .= esc_html__('Message: ') . $form_message . "<br>"; | |
$nfrom = 'From: <noreply@'. $home_url['host'] .'>' . "\r\n"; | |
$nreply = 'Reply-To:' .'<noreply@'. $email_from .'>'."\r\n"; | |
$headers = $nfrom; | |
$headers .= "MIME-Version: 1.0\r\n"; | |
$headers .= "Content-Type: text/html; charset=UTF-8\r\n"; | |
$headers .= $nreply; | |
// sending mail | |
wp_mail( $email_to, $subject, $body, $headers ); | |
// showing message if mail is sended | |
$message_success = esc_html__('Thanks for your mail'); | |
wp_send_json_success( $message_success ); | |
} | |
wp_die(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function ($) { | |
var form = $('#ajax-form'); | |
$('#ajax-form input, #ajax-form textarea').on('blur', function () { | |
$('#ajax-form input, #ajax-form textarea').removeClass('error'); | |
$('.notification').remove(); | |
$('#form_submit').val('Send'); | |
}); | |
var options = { | |
url: ajax_form_object.url, | |
data: { | |
action: 'ajax_form_action', | |
nonce: ajax_form_object.nonce | |
}, | |
type: 'POST', | |
dataType: 'json', | |
beforeSubmit: function (xhr) { | |
// changing button text | |
$('#form_submit').val('Sending...'); | |
}, | |
success: function (request, xhr, status, error) { | |
if (request.success === true) { | |
// cheking if fields filled — sending data and changing button text | |
form.after('<div class="notification notification_accept">' + request.data + '</div>').slideDown(); | |
$('#form_submit').val('Send'); | |
} else { | |
// cheking if fields is empty and changing button text | |
$.each(request.data, function (key, val) { | |
$('.form_' + key).addClass('error'); | |
$('.form_' + key).after('<div class="notification notification_warning notification_warning_' + key + '">' + val + '</div>'); | |
}); | |
$('#form_submit').val('Something went wrong...'); | |
} | |
// reset form field if all is OK | |
$('#ajax-form')[0].reset(); | |
}, | |
error: function (request, status, error) { | |
$('#form_submit').val('Something went wrong...'); | |
} | |
}; | |
// form sending | |
form.ajaxForm(options); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wp_localize_script( 'gym-send-mail', 'ajax_form_object', array( | |
'url' => admin_url( 'admin-ajax.php' ), | |
'nonce' => wp_create_nonce( 'ajax-form-nonce' ), | |
) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment