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

时间:2015-01-26 16:42:33

标签: wordpress plugins contact-form-7

我有以下代码作为一个函数将表单数据添加到数据库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);
}

2 个答案:

答案 0 :(得分:0)

您的代码存在的问题是$ 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,则为空)。

答案 1 :(得分:0)

代码实际上有效,因为它与“WP作业管理器 - 联系人列表”插件发生冲突,导致其无法正常工作