Created
August 19, 2022 17:56
-
-
Save Mamikonars/fd878abf669cb744cd96986333fba919 to your computer and use it in GitHub Desktop.
WordPress subscription 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
wp_enqueue_script( 'gym-subscribe', get_template_directory_uri() . '/assets/js/subscribe-form.js', array('jquery'), THEME_VERSION, true ); | |
wp_localize_script( 'gym-subscribe', 'subscribe_form_object', array( | |
'url' => admin_url( 'admin-ajax.php' ), | |
'nonce' => wp_create_nonce( 'subscribe-form-nonce' ), | |
) ); |
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_subscribe_form_action', 'subscribe_action_callback' ); | |
add_action( 'wp_ajax_nopriv_subscribe_form_action', 'subscribe_action_callback' ); | |
function subscribe_action_callback() { | |
// errors array | |
$errors = []; | |
// checking nonce | |
if ( !wp_verify_nonce( $_POST['nonce'], 'subscribe-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 mail field | |
if ( empty( $_POST['subscribe-form__email'] ) || !isset( $_POST['subscribe-form__email'] ) ) { | |
$errors['mail'] = esc_html__('Please enter your email'); | |
} else { | |
$form_mail = sanitize_text_field( $_POST['subscribe-form__email'] ); | |
} | |
// 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__('Email:') . $form_mail . '<br>'; | |
$headers = 'From: ' . $home_url['host'] . ' <' . $email_from . '>' . "<br>" . 'Reply-To: ' . $email_from; | |
// sending mail | |
wp_mail( $email_to, $subject, $body, $headers ); | |
/* adding mail data into custom post type post */ | |
$post_data = array( | |
'post_title' => $form_mail, | |
'post_status' => 'publish', | |
'post_author' => 1, | |
'post_type' => 'subscription', | |
); | |
wp_insert_post( $post_data ); | |
// showing message if mail is sended | |
$message_success = esc_html__('Thanks for your subscription'); | |
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 = $('#subscribe-form'); | |
$('#subscribe-form input').on('click', function () { | |
$('#subscribe-form input').removeClass('error'); | |
$('.notification').remove(); | |
$('#subscribe-form__btn').val('Subscribe'); | |
}); | |
var options = { | |
url: subscribe_form_object.url, | |
data: { | |
action: 'subscribe_form_action', | |
nonce: subscribe_form_object.nonce | |
}, | |
type: 'POST', | |
dataType: 'json', | |
beforeSubmit: function (xhr) { | |
// changing button text | |
$('#subscribe-form__btn').val('Subscribing...'); | |
}, | |
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(); | |
$('#subscribe-form__btn').val('Subscribe'); | |
} 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>'); | |
}); | |
$('#subscribe-form__btn').val('Something went wrong...'); | |
} | |
// reset form field if all is OK | |
$('#subscribe-form')[0].reset(); | |
}, | |
error: function (request, status, error) { | |
$('#subscribe-form__btn').val('Something went wrong...'); | |
} | |
}; | |
// form sending | |
form.ajaxForm(options); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment