Skip to content

Instantly share code, notes, and snippets.

@campusboy87
Last active March 30, 2022 20:49
  • Star 5 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save campusboy87/a056c288c99feee70058ed24cee805ad to your computer and use it in GitHub Desktop.
Displays a message about the successful submission of the form in a modal window for the Contact Form 7 plugin
<?php
/**
* Plugin Name: CF7 Modal Invalid 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_invalid_js' );
add_action( 'wp_footer', 'wpcf7_modal_invalid_js_inline', 999 );
/**
* Поключает библиотеку sweetalert.js для создания красивых модальных окон.
*
* @link https://sweetalert.js.org/
*
* @return void
*/
function wpcf7_modal_invalid_js() {
wp_enqueue_script( 'sweetalert', 'https://unpkg.com/sweetalert/dist/sweetalert.min.js' );
}
/**
* Выводит на экран модальное окно при ошибках валидации формы.
*
* @return void
*/
function wpcf7_modal_invalid_js_inline() {
?>
<script>
// Срабатывает при ошибках валидации формы.
document.addEventListener('wpcf7invalid', function (response) {
// Запускает модальное окно.
swal({
title: "Ошибка!",
text: response.detail.apiResponse.message,
icon: "error",
button: "Закрыть"
});
}, false);
</script>
<style>
.wpcf7-validation-errors {
display: none !important;
}
</style>
<?php
}
<?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_mailsent_js' );
add_action( 'wp_footer', 'wpcf7_modal_mailsent_js_inline', 999 );
/**
* Поключает библиотеку sweetalert.js для создания красивых модальных окон.
*
* @link https://sweetalert.js.org/
*
* @return void
*/
function wpcf7_modal_mailsent_js() {
wp_enqueue_script( 'sweetalert', 'https://unpkg.com/sweetalert/dist/sweetalert.min.js' );
}
/**
* Выводит на экран модальное окно при успешной отправки формы.
*
* @return void
*/
function wpcf7_modal_mailsent_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
}
@campusboy87
Copy link
Author

example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment