【问题标题】:Wordpress Help Redirect after form submission表单提交后的 Wordpress 帮助重定向
【发布时间】:2014-03-13 21:00:32
【问题描述】:

我希望在提交表单后重定向我的联系表单。我不知道怎么做。这是我的代码,它也给了我一个错误。

<?php

// If the form is submitted
if(isset($_POST['submit'])) {

    // Include WordPress Core Functions
    $wp_include = '../wp-load.php';
    while(!@include_once($wp_include)) { $wp_include = '../'.$wp_include; }


    //
    // Field Validation
    //

    // Check to make sure that the name field is not empty
    if(trim($_POST['cf_name']) == '') {
        $has_error = true;
    }
    else {
        $name = trim($_POST['cf_name']);
    }

    // Check to make sure that the subject field is not empty
    if(trim($_POST['cf_subject']) == '') {
        $has_error = true;
    } 
    else {
        $subject = trim($_POST['cf_subject']);
    }

    // Check to make sure sure that a valid email address is submitted
    if(trim($_POST['email']) == '')  {
        $has_error = true;
    } 
    elseif(!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i", trim($_POST['email']))) {
        $has_error = true;
    }
    else {
        $email = trim($_POST['email']);
    }

    // Check to make sure comments were entered
    if(trim($_POST['cf_message']) == '') {
        $has_error = true;
    } 
    else {
        if(function_exists('stripslashes')) {
            $message = stripslashes(trim($_POST['cf_message']));
        }
        else {
            $message = trim($_POST['cf_message']);
        }
    }


    //
    // Send E-Mail
    //

    // Send the email if there is no error
    if(!isset($has_error)) {
        // Get recheiver
        $receiver = ($_POST['cf_receiver']) ? $_POST['cf_receiver'] : get_option('admin_email');
        $receiver = str_replace('[at]', '@', $receiver);

        // Headers
        $headers = "From: $name <$email>\n";
        $headers.= "Content-Type: text/plain; charset=\"UTF-8\"\n";

        // Message
        if($_POST['cf_email_signature'] && $_POST['cf_email_signature'] != 'none') {
            $message.= "\n\n---\n".$_POST['cf_email_signature'];
        }

        // Send E-Mail
        $mail_sent = wp_mail($receiver, $subject, $message, $headers);
        if($mail_sent)
            echo "<p class='info-box success'>".$_POST['cf_success_msg']."</p>";
        else
            echo "<p class='info-box error'>The message couldn't be sent because an internal error occured.</p>";

        echo "<p class='info-box error'>".$_POST['cf_error_msg']."</p>";
    }
}

?>

【问题讨论】:

  • 谢谢您的评论我已经修复了错误

标签: php wordpress forms post


【解决方案1】:

wordpress 中的重定向函数是wp_redirect,例如

if( $mail_sent ) {
    wp_redirect( '/my-target' );
    exit();
} else {
    //output your warning
}

【讨论】:

  • 我应该把这个放在哪里?
  • 在脚本的末尾。替换你的if( $mail_sent )
猜你喜欢
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 1970-01-01
相关资源
最近更新 更多