2

First off let me just say, I'm new to theme development, I hope there isn't something obvious I'm missing.

I'm trying to get this form

contactform.php

<form method="POST" action="">
    <input type="text" name="contact_name" placeholder="Name" class="form-control" required>
    <input type="email" name="contact_email" placeholder="E-Mail" class="form-control" required>
    <textarea name="contact_message" placeholder="Message" rows="4" class= "form-control"  required></textarea>
  <button type="submit" class="btn btn-secondary">Send</button>
</form>

<?php 
//process request...

//redirect to previous page
global $wp;
wp_redirect(home_url($wp->request));
exit;
?> 

Into the footer like so:

footer.php

<footer class="footer">
    <div class="container">           
        <div class="row">
            <div class="col-sm">
                Stuff
            </div>
            <div class="col-sm">
                Stuff
            </div>
            <div class="col-sm">
                <!-- form goes here -->
                <?php get_template_part( 'contactform'); ?>
            </div>
        </div>
        <div class="row">
            <div class="col-sm">More stuff</div>
            <div class="col-sm">More stuff</div>
            <div class="col-sm">More stuff</div>
        </div>
    </div>
<?php wp_footer(); ?>
</footer>

The problem is, the second row is missing. It's not in the DOM. But when I hardcode the form into the footer it works fine. So I figured there's a problem with the get_template_part function.

0

It looks like the code in contactform.php may be causing issues.

These lines, specifically:

global $wp;
wp_redirect(home_url($wp->request));
exit;

The exit statement may be stopping execution of the rest of the script. If you want to process form data, you should be checking for it and processing it instead of just running the redirect and exit statements whenever the part is included.

  • I am. I do stuff with the post variables, send email to admin with wp_mail and then I redirect back – Žiga Stupan Sep 21 '18 at 19:33
  • @ŽigaStupan so you are doing something like if ( should_process_post_data ) { ... exit; }? Is it possible to include more of the contactform.php file? – phatskat Sep 21 '18 at 19:36
  • 1
    You were right, I put the redirect outside the conditional checking if the $_POST variables exist so it always executed. It was an obvious mistake after all. Thank you – Žiga Stupan Sep 21 '18 at 19:38
  • The form processing functions really don't belong in the template with the form. They should be in the functions.php file and hooked into the page load. – Jacob Peattie Sep 22 '18 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.