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


PHP code to redirect contact form to thank you page

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

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

通过创建感谢页面,当按钮提交时,它将重定向,以跟踪填写联系表单的人数,以便我们看到什么是在AdWords活动中起作用。

我发现了一段代码(wp_redirect(get_bloginfo('wpurl').'/your-thank- 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(); ?>

添加:

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

行之后
        $emailSent = true;

我在这里重定向

 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 
}

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

你可以把ini_set('display_errors',1);在顶部与它一起工作,我有一种感觉,wordPress不会让你重定向页面(因为get_header()输出页面一旦被调用,你的代码可能在那之后运行)。

这里有一篇关于这方面的更多信息。

Wordpress重定向问题,标题已发送