简体   繁体   English

通过Functions.php重定向WordPress联系表格7

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

I have the following code as a function to add the form data to the database usermeta table then send an email which all works. 我有以下代码,可以将表单数据添加到数据库usermeta表中,然后发送所有有效的电子邮件。 The issue is the form stays stuck on the loading image and I can't find anyway to get it to redirect or display a confirmation message, any help would be much appreciated. 问题是表单停留在加载图像上,我无法找到使其重定向或显示确认消息,我们将不胜感激。 I've tried using another function with wpcf7_mail_sent but nothing happens, tried additional settings for the form and am stuck. 我尝试将另一个功能与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);
}

The problem with your code is that $contact_form isn't defined. 您的代码存在的问题是未定义$ contact_form。

You can use something like this: 您可以使用如下形式:

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);

    }

}

Also made the code more readable and fixed some other issues (empty if conditional and undefined $header). 还使代码更具可读性,并解决了其他一些问题(如果有条件且未定义$ header,则为空)。

该代码实际上可以正常工作,因为它与阻止其工作的“ WP Job Manager-联系人列表”插件发生冲突

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM