【问题标题】:PHP code to redirect contact form to thank you page将联系表单重定向到感谢页面的 PHP 代码
【发布时间】:2014-07-04 17:55:00
【问题描述】:

我希望有人可以帮助我解决以下问题。我想在 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(); ?>

【问题讨论】:

  • 您是否在电子邮件发送后尝试重定向浏览器?
  • 是的,Alexander,这就是意图。你会推荐什么?

标签: php wordpress forms redirect contact


【解决方案1】:

添加:

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

就在这条线之后

        $emailSent = true;

【讨论】:

  • $emailSent = true;重定向后没有意义更好地执行 exit();
  • 嗨,J,感谢您的快速回复。我需要页面的完整路径,即example.com/get-in-touch/thanks
  • @user3806207,HTTP 规范说您应该使用完整的绝对 URL。大多数浏览器都会接受相对 URL,但我建议您使用完整 URL。
  • 干杯 J,非常感谢 :)
  • 嘿,J,尝试按照您的建议添加代码,但所发生的只是我被重定向到与联系表单相同的页面(即example.com/get-in-touch)而不是example.com/thanks。我在下面添加了代码 $headers = 'From: '.$name.' ' 。 "\r\n" 。 '回复: ' 。 $电子邮件;邮件($emailTo,$subject,$body,$headers); $emailSent = 真; http_redirect("example.com/thanks");** 重定向代码到感谢页面 */ } } ?>
【解决方案2】:

我在这里做重定向

 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

【讨论】:

  • 感谢 ArtisiticPhoenix,非常感谢 :)
猜你喜欢
  • 2012-11-11
  • 2013-09-10
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多