TODO: To change the header's content go to Dr.Explain menu Options : Project Settings : HTML (CHM) Export : Setup HTML Template and Layout
×
Menu
Index

Cấu hình thông tin form

Trong hooks sử lý dữ liệu gửi đi từ form bởi người dùng () bạn sẽ xác định form nào và lấy dữ liệu của người dùng nhập, đối tượng $WPCF7_ContactForm gửi vào hàm callback sử lý hook 'wpcf7_before_send_mail', lấy thông tin ở form hiện tại:

add_action('wpcf7_before_send_mail','_wpcf7_before_send_mail');
function _wpcf7_before_send_mail($WPCF7){
/**
    * lấy id của form
    */
    $form_id=$WPCF7->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 = $WPCF7->id; // Wrong.
 
//xác định tên form
$WPCF7->title();
 
//lấy đối tượng form hiện tại = $WPCF7
$current = WPCF7_ContactForm::get_current() ;
}
 
+ Chặn gửi email: mặc định wpcf7 gửi mail sau khi các giá trị trường của form thỏa mãn điều kiện, để tắt chế độ gửi mail thiết lập 'true' cho thuộc tính 'skip_mail'.
add_action( 'wpcf7_before_send_mail', 'wpcf7_disablEmailAndRedirect' );
 
function wpcf7_disablEmailAndRedirect( $cf7 ) {
     // do not send the email
    $wpcf7->skip_mail = true;
}
 
+ Thuộc tính cài đặt từ form:
Lấy thuộc tính cài đặt trên form. vd như: mail, mail_2, messages…
/* So, use prop() method to access them.
Biến $form chứa giá trị chính là các nội dung messages thiết lập trong admin.
*/
$form = $WPCF7_ContactForm->prop( 'messages' );  #kết quả cho ra các chuỗi tin nhắn đối ứng với từng TH form gặp phải như lỗi form (validation error)
 
Cập nhật thuộc tính chúng ta sử dụng phương thức set_properties. Xem đoạn code sau:
/* To set the properties, use set_properties() method, like this: */
$mail = $WPCF7_ContactForm->prop( 'mail' );
$mail['subject'] = "Well, hello, Dolly";
$WPCF7_ContactForm->set_properties( array( 'mail' => $mail ) );
s
 
Addition settings:
Uage:
Mỗi option bạn viết trên một dòng.
Tùy chọn cài đặt này được lưu trên form , bạn có thể truy xuất giá trị của nó thông qua phương thức 'additional_setting'.
//get all Additional Settings
$current->prop('additional_settings');
 
//get one setting
$current = WPCF7_ContactForm::get_current() ;
$opt=$current->additional_setting( 'option1', false );
 
Lấy toàn bộ settings hoặc từng setting xác định bởi tên. Kết quả trả về mảng chứa giá trị setting.
Array
(
    [0] => "value option1";
)
 
Trong các trường hợp bạn muốn cập nhật động cài đặt settings chúng ta có phương thức 'set_properties'.
$wpcf7->set_properties( array(
        'additional_settings' => "on_sent_ok: \"location.replace('http://example.com//');\"",
    ) );
 
 
Made with help of Dr.Explain

Unregistered version