Skip to content

Instantly share code, notes, and snippets.

@RiodeJaneiroo
Last active March 26, 2019 13:44
Show Gist options
  • Save RiodeJaneiroo/d230a1d80bbbcb0ddd8fbd952e812131 to your computer and use it in GitHub Desktop.
Save RiodeJaneiroo/d230a1d80bbbcb0ddd8fbd952e812131 to your computer and use it in GitHub Desktop.
[wpcf7 modal right answer] Displays a message about the successful submission of the form in a modal window for the Contact Form 7 plugin #wordpress #CF7
<?php
/**
* Plugin Name: CF7 Modal Right Answer
* Plugin URI: https://gist.github.com/campusboy87/a056c288c99feee70058ed24cee805ad
* Author: Campusboy (wp-plus)
* Author URI: https://www.youtube.com/wp-plus
*/
add_action( 'wp_enqueue_scripts', 'wpcf7_modal_right_answer_js' );
add_action( 'wp_footer', 'wpcf7_modal_right_answer_js_inline', 999 );
/**
* Поключает библиотеку sweetalert.js для создания красивых модальных окон.
*
* @link https://sweetalert.js.org/
*
* @return void
*/
function wpcf7_modal_right_answer_js() {
wp_enqueue_script( 'sweetalert', 'https://unpkg.com/sweetalert/dist/sweetalert.min.js' );
}
/**
* Выводит на экран модальное окно при успешной отправки формы.
*
* @return void
*/
function wpcf7_modal_right_answer_js_inline() {
?>
<script>
// Срабатывает при успешной отправке формы.
document.addEventListener('wpcf7mailsent', function (response) {
// Запускает модальное окно.
swal({
title: "Спасибо!",
text: response.detail.apiResponse.message,
icon: "success",
button: "Закрыть"
});
}, false);
</script>
<style>
.wpcf7-mail-sent-ok {
display: none !important;
}
</style>
<?php
}
// Magnific popup show message success send CF7;
add_action( 'wp_enqueue_scripts', 'wpcf7_modal_right_answer_js' );
add_action( 'wp_footer', 'wpcf7_modal_right_answer_js_inline', 999 );
/**
* Поключает библиотеку sweetalert.js для создания красивых модальных окон.
*
* @link https://sweetalert.js.org/
*
* @return void
*/
function wpcf7_modal_right_answer_js() {
if ( ! wp_script_is( 'magnific-popup-js', 'enqueued' ) ) {
wp_register_script( 'magnific-popup-js', get_template_directory_uri().'/js/magnific-popup.min.js', array('jquery'), '', true );
wp_enqueue_script( 'magnific-popup-js' );
}
if ( ! wp_style_is( 'magnific-popup-css', 'enqueued' ) ) {
wp_register_style( 'magnific-popup-css', get_template_directory_uri().'/css/magnific-popup.css');
wp_enqueue_style( 'magnific-popup-css' );
}
}
/**
* Выводит на экран модальное окно при успешной отправки формы.
*
* @return void
*/
function wpcf7_modal_right_answer_js_inline() {
?>
<script>
// Срабатывает при успешной отправке формы.
document.addEventListener('wpcf7mailsent', function (response) {
$.magnificPopup.close();
// Запускает модальное окно.
$.magnificPopup.open({
removalDelay: 400,
mainClass: 'mfp-fade',
items: {
src: '<div class="white-popup">' + response.detail.apiResponse.message + '</div>',
type: 'inline'
}
});
setTimeout("$.magnificPopup.close()", 4000);
}, false);
document.addEventListener('wpcf7submit', function (event) {
var api = event.detail.apiResponse;
console.log('first');
if (api.status === 'validation_failed') {
console.log(api);
var classEl = api.invalidFields[0].into;
jQuery('input, textarea', classEl).focus();
}
else {
sendFormOne = true;
}
}, false);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment