Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * functions.php
- */
- // Zarejestruj endpoint do wysyłania potwierdzenia (lepiej zmodyfikuj treść)
- add_action( 'rest_api_init', function () {
- register_rest_route( 'form-confirm/v1', '/send', array(
- 'methods' => 'POST',
- 'callback' => function ( $request ) {
- // Pobierz i oczyść adres e-mail
- $email = filter_var( $request->get_param( 'email' ), FILTER_SANITIZE_EMAIL );
- // Wyślij maila
- $subject = 'Potwierdzenie wysłania wiadomości';
- $message = 'Pojszło! I chu.';
- $result = wp_mail( $email, $subject, $message );
- // Wyślij odpowiedź do JS - czy się mail został wysłany
- return array( 'sent' => $result );
- },
- ) );
- } );
- /*
- * W ramach wp_enqueue_scripts
- */
- // Załaduj plik JS - jakkolwiek chcesz
- wp_enqueue_script( 'form_confirm', get_theme_file_uri( '/form-confirm.js' ), array( 'jquery' ) );
- // Przekaż url endpointu do skryptu jako zmienną JS; pierwszy argument musi być taki sam, jak w wp_enqueue_script
- wp_localize_script( 'form_confirm', 'form_confirm', array(
- 'rest_url' => home_url( '/wp-json/form-confirm/v1/send' ),
- ) );
- /*
- * W pliku JS
- */
- (function($) {
- // Prosi back-end o wysłanie potwierdzenia na dany adres e-mail
- function sendConfirmationEmail(email) {
- $.post(window.form_confirm.rest_url, { email: email }, function(res) {
- console.log(res);
- });
- }
- // Podpina wysłanie potwierdzenia pod submit event podanego formularza (obiekt jQuery)
- function confirmOnSubmit($form) {
- $form.on('submit', function() {
- var email = $form.find('[data-field_type="email"]').val();
- if (email) {
- sendConfirmationEmail(email);
- }
- });
- }
- // Po załadowaniu dokumentu podepnij wysyłanie potwierdzenia pod wszystkie formularze Divi
- $(function() {
- $('.et_pb_contact_form').each(function() {
- confirmOnSubmit($(this));
- });
- });
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment