Mình muốn bắt sự kiện sau khi khách ấn Submit xong trong form liên hệ của Contact form 7 thì như thế nào nhỉ mọi người, nghĩa là sau khi khách hàng điền thông tin hợp lệ thành công và ấn submit ok thì web sẽ chuyển hướng sáng trang cảm ơn (trang thank you) thì làm sao. Bạn nào có cái code cho mình với nhé, hướng dẫn chi tiết giúp, cảm ơn.
Bạn add code javascript sau vào header hay footer của web nhé: HTML: document.addEventListener( 'wpcf7submit', function( event ) { if ( event.detail.contactFormId == '111' && event.detail.status=='mail_sent') { //Thay 111 thành ID của form liên hệ location = 'https://itseovn.com/cam-on'; //Thay thành link cần chuyển hướng } }, false );
Còn muốn bài bản thì add vào file javascript sau đó add vào function.php của themes hơi phức tạp mà sau này dễ quản lý thì làm như sau: Bước 1: Tạo 1 file js với tên: itseovn.js và copy code sau bỏ vào. Tiếp theo copy file này bỏ vào mục gốc của themes: /js/itseovn.js HTML: jQuery(document).ready(function () { document.addEventListener( 'wpcf7submit', function( event ) { if ( event.detail.contactFormId == '111' && event.detail.status=='mail_sent') { //Thay 111 thành ID của form location = 'https://itseovn.com/cam-on'; //Thay thành link cần chuyển hướng } }, false ); }); Bước 2: Vào function.php của themes thêm code sau vào để khai báo file (bước này là bước khai báo file js kia cho wordpress biết lấy ra sử dụng mỗi khi chạy web) PHP: //load javascript cho web add themadd_action('after_setup_theme', 'itseovn_theme_setup');if ( ! function_exists( 'itseovn_theme_setup' ) ){ function itseovn_theme_setup(){ add_action( 'wp_enqueue_scripts', 'itseovn_ajax_script'); }}//khai báo file mới tạo tên itseovnajax.js cho wordpress cho vào webfunction itseovn_ajax_script() { wp_enqueue_script('itseovn_ajax', get_stylesheet_directory_uri() . '/js/itseovn.js', array(), '1.0.0', true ); wp_localize_script( 'itseovn_ajax', 'ajax_obj', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'smart_nonce' => wp_create_nonce( 'itseovnajax-nonce' ) ) );}