簡體   English   中英

通過Functions.php重定向WordPress聯系表格7

[英]WordPress Contact Form 7 Redirect via Functions.php

我有以下代碼,可以將表單數據添加到數據庫usermeta表中,然后發送所有有效的電子郵件。 問題是表單停留在加載圖像上,我無法找到使其重定向或顯示確認消息,我們將不勝感激。 我嘗試將另一個功能與wpcf7_mail_sent一起使用,但沒有任何反應,嘗試了表單的其他設置並被卡住。

add_action('wpcf7_before_send_mail', 'cf7import',1);
function cf7import() {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();
if ( $submission ) 
{
$posted_data = $submission->get_posted_data(); 
$formtitle = $cfdata->title; } 
if ( $formtitle == 'Apply Form') { 
}
 global $wpdb; 
 $user_id = get_current_user_id();
 update_user_meta( $user_id, 'prefix', $posted_data['prefix'] );
 update_user_meta( $user_id, 'first_name', $posted_data['first-name'] );
 update_user_meta( $user_id, 'middle_name', $posted_data['middle-name'] );

global $current_user;
 get_currentuserinfo();
 $email_address = 'contact@website.com';
 // write the email content
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=utf-8\n";
$header .= "From:" . $email_address;
$subject = 'New Application Form';
$message = "Hi,<br/><br/>".$posted_data['first-name'].' '.$posted_data['last-name']."<br/><br/>";
$message .= "Your application has been submitted successfully";
wp_mail($current_user->user_email, $subject, $message, $header);
}

您的代碼存在的問題是未定義$ contact_form。

您可以使用如下形式:

add_action('wpcf7_before_send_mail', 'cf7import', 1);

function cf7import($contact_form) {

    $submission = WPCF7_Submission::get_instance();

    if ( $submission ){
        $posted_data = $submission->get_posted_data(); 
        $formtitle   = $contact_form->title(); 
    } 

    if ( $formtitle == 'Apply Form') { 

        global $wpdb, $current_user; 

        $user_id = get_current_user_id();
        update_user_meta( $user_id, 'prefix', $posted_data['prefix'] );
        update_user_meta( $user_id, 'first_name', $posted_data['first-name'] );
        update_user_meta( $user_id, 'middle_name', $posted_data['middle-name'] );

        get_currentuserinfo();

        $email_address = 'contact@website.com';

        // write the email content
        $header = "MIME-Version: 1.0\n";
        $header .= "Content-Type: text/html; charset=utf-8\n";
        $header .= "From:" . $email_address;

        $subject = 'New Application Form';

        $message = "Hi,<br/><br/>".$posted_data['first-name'].' '.$posted_data['last-name']."<br/><br/>";
        $message .= "Your application has been submitted successfully";
        wp_mail($current_user->user_email, $subject, $message, $header);

    }

}

還使代碼更具可讀性,並解決了其他一些問題(如果有條件且未定義$ header,則為空)。

該代碼實際上可以正常工作,因為它與阻止其工作的“ WP Job Manager-聯系人列表”插件發生沖突

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM