Sự kiện Form.
Dữ liệu sau khi người dùng nhấn submit trên form sẽ gửi vào mail mà bạn cấu hình cho mỗi form. Tuy nhiên để có thể gửi mail thành công trong wordpress bạn cần cấu hình gửi email bằng SMTP. Chúng ta cần cài thêm plugin WP SMTP bạn có thể gửi mail sử dụng hàm mail() trong php hoặc sử dụng SMTP riêng với tài khoản miễn phí từ Yahoo, gmail..
wpcf7 có một số hooks giúp bạn bắt dữ liệu gửi đi thông qua phương thức POST.
function wpcf7_do_something ($WPCF7_ContactForm) {
/* Use WPCF7_Submission object's get_posted_data() method to get it. */
/**
* lấy id của form
*/
$form_id=$WPCF7_ContactForm->id(); //ie: 20
//note: đối với một số phiên bản cũ, không còn được sử dụng. VD:
/* Don't do this, since id property is no longer accessible. */
$id = $contact_form->id; // Wrong.
//get form title
$WPCF7_ContactForm->title();
}
add_action("wpcf7_before_send_mail", "wpcf7_do_something");
|
Lấy dữ liệu form chúng ta có phương thức 'get_posted_data()'. Trong hàm sử lý dữ liệu form bởi hook 'wpcf7_before_send_mail'. Kiểm tra form đã được nhấn submit, nếu giá trị đã được khởi tạo WPCF7_Submission::get_instance();
function wpcf7_do_something ($WPCF7_ContactForm) {
/* Use WPCF7_Submission object's get_posted_data() method to get it. */
$submission = WPCF7_Submission::get_instance();
if($submission){
$posted_data = $submission->get_posted_data();
...
}
}
|
Cặp key-value đại diện cho các nội dung trường có trên form chứa trong biến $posted_data.
Bạn có thể lấy thông tin này và lưu ra bên ngoài, ví dụ sau đây mình sẽ sử dụng để lưu vào google form. Rất đáng tin cậy!
function wpcf7_do_something ($WPCF7_ContactForm) {
/* Use WPCF7_Submission object's get_posted_data() method to get it. */
$submission = WPCF7_Submission::get_instance();
$gform_id = 'ưo239wetlergptwe';
if ( $submission ) {
$posted_data = $submission->get_posted_data();
//save form data
$url_action = 'https://docs.google.com/forms/d/'.$gform_id.'/formResponse';
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url_action);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); #ssl
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$posted_data);
curl_exec($ch);
}
}
add_action("wpcf7_before_send_mail", "wpcf7_do_something");
|
Note:
-
lưu giá ID của google form vào settings của form hiện tại đang hoạt động.
Sau khi form đã sử lý hoàn tất, email đã được gửi đến người nhận bạn chuyển hướng user đến trang cảm ơn. Chỉ định form và redirect vào URL bạn mong muốn.
Chúng ta viết trong hook 'wpcf7_mail_sent'.
function redir_after_form_sent($form) {
if ($form->title == "Ask a question") {
header('Location: /thank-you');
die();
}
}
add_action('wpcf7_mail_sent', 'redir_after_form_sent');
|
Sự kiện Javascript.
Các sự kiện javascript đi cùng với hành động của form sẽ được lưu vào cài đặt Additional settings theo cấu trúc:
theo cấu trúc: event_name:'js-statements'
on_sent_ok: "location = 'http://example.com/';"
|
- Sự kiện on_sent_ok: xẩy ra khi form gửi hoàn tất dữ liệu. Bạn cũng có thể lấy nội dung sự kiện này trong mã php.
$on_sent_ok=$WPCF7_ContactForm->additional_setting( 'on_sent_ok' );
|
- Sự kiện on_submit: không cần biết form có hoạt động không (thành công hay thất bại) miễn sau khi nhấn submit sẽ gọi sự kiện này.
on_submit:'console.log("abcvsđfdf")'
|
Thêm nội dung json vào kết quả trả về của form bởi ajax.
Kết quả trả về của form là chuỗi json sau khi nhấn nút submit form bằng cách sử lý dữ liệu mặc định thông qua ajax. VD: nội dung json lỗi.
{"mailSent":false,"into":"#wpcf7-f310-p212-o1","captcha":null,"message":"Failed to send your message. Please try later or contact the administrator by another method."}
|
Bạn có thể thêm nội dung mới vào chuỗi json này, bằng cách sử dụng hook wpcf7_ajax_json_echo. Ví dụ sau đây lấy nội dung form settings vào kết quả trả về của form.
add_filter( 'wpcf7_ajax_json_echo', 'ajax_results_append',10,2);
function ajax_results_append($items,$result){
$current = WPCF7_ContactForm::get_current() ;
$items['option1'] =$current->additional_setting( 'option1', false );
// send it back because it's a filter
return $items;
}
|
Đoạn code trên, mình chèn thêm biến option1 của đối tượng json trả về bởi ajax. Tham số $items là mảng kết quả của form và được mã hóa với hàm json_encode thành chuỗi json. Mặc định $items chứa kết quả + nội dung sự kiện js của WPCF7 bạn có thiết lập và các biến tạo thêm giống như đoạn code ở trên.
tham số thứ 2 $result, lưu kết quả thông báo sau khi sử lý form. Có nội dung mẫu như sau:
Array
(
[status] => mail_failed
[message] => Failed to send your message. Please try later or contact the administrator by another method.
[demo_mode] =>
[scripts_on_submit] => Array
(
[0] => location = 'http://example.com/';
)
)
|
note: 'scripts_on_submit' gồm: on_sent_ok, on_submit) sẽ khai báo tại biến $result này.