简体   繁体   中英

PHP code to redirect contact form to thank you page

I hope someone may help me with the following issue. I want to create a new page in WordPress (ie http://www.example.com/get-in-touch/thanks )

The reason for this is when the contact form is filled in on http://www.example.com/get-in-touch it doesn't redirect to another page.

By creating the thank you page, when the button is submitted it will redirect in order to track the amount of people that fill in the contact form so we see what is working in an AdWords campaign.

I have found a piece of code (wp_redirect(get_bloginfo('wpurl').'/your-thank-you-page/'); exit;) that is suggested but I'm unsure how this will work or where to fit it into the code?

I've included the code below for the contact form…...

I appreciate any suggestions !!!!!!

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

<?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(); ?>

Add:

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

just after the line

        $emailSent = true;

Id do the redirect here

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

but like this

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

one issue you may run into is header redirect after outputting content. If that happens you can echo a small bit of javascirpt ( like window.location.href ) to redirect the page, and call exit(). Just depends if wordpress has already output the top of the page, as is pretty common.

you might want to put ini_set('display_errors' ,1); at the top while working with it, i have a feeling that wordPress wont let you redirect the page ( because get_header() outputs the page as soon as it is called and you code may be running after that ).

here is a post with more info on that.

Wordpress redirect issue, headers already sent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM