Problem with using get_template_part() in footer

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.

1 Answer
1

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.

Leave a Comment