PHP代码将联系表单重定向到感谢页面

时间:2014-07-04 17:55:00

标签: php wordpress forms redirect contact

我希望有人可以帮我解决以下问题。我想在WordPress中创建一个新页面(即http://www.example.com/get-in-touch/thanks

原因是联系表单填写在http://www.example.com/get-in-touch时,它不会重定向到另一页。

通过创建感谢页面,当提交按钮时,它将重定向,以便跟踪填写联系表单的人数,以便我们查看AdWords广告系列中的工作内容。

我找到了一段代码(wp_redirect(get_bloginfo('wpurl')。'/ your-thank-you-page /');      出口;) 建议,但我不确定这将如何工作或在哪里适合代码?

我在联系表单中包含了以下代码......

我感谢任何建议!!!!!!

=============================================== ===============================

<?php
/*
* Template Name: Contact Form
*/

$nameError = __( 'Please enter your name.', 'stag' );
$emailError = __( 'Please enter your email address.', 'stag' );
$emailInvalidError = __( 'You entered an invalid email address.', 'stag' );
$commentError = __( 'Please enter a message.', 'stag' );

$errorMessages = array();

if(isset($_POST['submitted'])){
    if(trim($_POST['contactName']) === '') {
        $errorMessages['nameError'] = $nameError;
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }

    if(trim($_POST['email']) === '')  {
        $errorMessages['emailError'] = $emailError;
        $hasError = true;
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
        $errorMessages['emailInvalidError'] = $emailInvalidError;
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    if(trim($_POST['comments']) === '') {
        $errorMessages['commentError'] = $commentError;
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['comments']));
        } else {
            $comments = trim($_POST['comments']);
        }
    }

    if(!isset($hasError)) {
        $emailTo = stag_get_option('general_contact_email');
        if (!isset($emailTo) || ($emailTo == '') ){
            $emailTo = get_option('admin_email');
        }
        $subject = '[Contact Form] From '.$name;

        $body = "Name: $name \n\nEmail: $email \n\Message: $comments \n\n";
        $body .= "--\n";
        $body .= "This mail is sent via contact form on ".get_bloginfo('name')."\n";
        $body .= home_url();

        $headers = 'From: '.$name.' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }
}
?>
<?php } else { ?>
<?php get_header(); ?>

<!--BEGIN #primary .hfeed-->
<div id="primary" class="hfeed" role="main">

<?php while(have_posts()): the_post(); ?>

    <?php stag_page_before(); ?>
    <!--BEGIN .hentry-->
    <article <?php post_class(); ?>>
    <?php stag_page_start(); ?>

<h1 class="entry-title"><?php the_title(); ?></h1>

        <!-- BEGIN .entry-content -->
        <div class="entry-content">
            <?php
                the_content( __('Continue Reading', 'stag') );
                wp_link_pages(array('before' => '<p><strong>'.__('Pages:', 'stag').'</strong> ', 'after' => '</p>', 'next_or_number' => 'number'));
            ?>
        <!-- END .entry-content -->
        </div>

    <?php stag_page_end(); ?>
    <!--END .hentry-->
    </article>

    <?php stag_page_after(); ?>

<?php endwhile; ?>

<h5 style="font-size: 130% !important" id="reply-title">Send us a message</h5>

<?php if(isset($emailSent) && $emailSent == true) { ?>

    <div class="stag-alert accent-background">
        <p><?php _e('Thanks, your email was sent successfully.', 'stag') ?></p>
    </div>
<form action="<?php the_permalink(); ?>" id="contactForm" class="contact-form" method="post">

    <div class="grids">
        <p class="grid-6">
            <label for="contactName"><?php _e('Your Name', 'stag') ?></label>
            <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>">
        </p>

        <p class="grid-6">
            <label for="email"><?php _e('Your Email', 'stag') ?></label>
            <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>">
        </p>
    </div>

    <p>
        <label for="commentsText"><?php _e('Your Message', 'stag') ?></label>
        <textarea rows="8" name="comments" id="commentsText" ><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
    </p>

    <p class="buttons">
        <input type="submit" id="submitted" class="accent-background contact-form-button" name="submitted" value="<?php _e('Send Message', 'stag') ?>">
    </p>

</form>

<?php } ?>

<!--END #primary .hfeed-->
</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

2 个答案:

答案 0 :(得分:0)

添加:

        http_redirect("path/to/the/thanks/page");

在行

之后
        $emailSent = true;

答案 1 :(得分:0)

我在这里重定向

 mail($emailTo, $subject, $body, $headers);

但是喜欢这个

if( mail($emailTo, $subject, $body, $headers) ){
   ///redirect
   ///$emailSent = true; this variable is pointless after a redirect.
   exit(); //call exit to prevent further script execution 
}

您可能遇到的一个问题是输出内容后的标题重定向。如果发生这种情况,您可以回显一小部分javascirpt(如window.location.href)来重定向页面,并调用exit()。仅仅取决于wordpress是否已经输出页面顶部,这很常见。

你可能想放入ini_set('display_errors',1);在使用它的顶部,我感觉wordPress不会让你重定向页面(因为get_header()一旦调用它就输出页面,你的代码可能会在那之后运行。)

这是一篇帖子,其中包含更多信息。

Wordpress redirect issue, headers already sent