【问题标题】:WordPress Contact Form 7 Redirect via Functions.phpWordPress 联系表格 7 通过 Functions.php 重定向
【发布时间】:2015-01-29 19:43:04
【问题描述】:

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

【问题讨论】:

    标签: wordpress plugins contact-form-7


    【解决方案1】:

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

    【讨论】:

    • 感谢您的帮助,但它仍然停留在加载 gif 中,没有页面确认表单已发送
    【解决方案2】:

    该代码实际上可以正常工作,因为它与阻止其工作的“WP Job Manager - Contact Listing”插件冲突

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2014-10-08
      相关资源
      最近更新 更多