I am working on a new theme with a similar issue. I have about 8 custom pages with forms that redirect on success.
For validation I submit the form pages to themselves, but then redirecting causes an error since page-XXX.php files are called after head.php.
I think the solution is to make a new base-page-XXX.php file for each of the custom pages and then just put wp_redirect before the head, but I’d like to know if there’s a better way. It almost defeats the purpose of having base.php when I have to copy it so many times.
function check_post_data() {
if ($_POST) {
// do logic
if (!errors) {
wp_redirect('the next step');
exit;
}
} // end $_POST actions
}
add_action('template_redirect', 'check_post_data');
You may need to run some specific checks on $_POST data or even is_page() depending on your structure.
You’re right that it’s not a Roots-specific question, but the issue is caused by Roots’ use of base.php. Normally get_header() would be called inside a page template, so adding a redirect or other code before it is simple. I was hoping there was a Roots process for doing what I’m describing, but since there isn’t then I’ll figure something out.
I tried using your suggestion, but since I need to pass a (potential) multitude of error/success/warning messages to the page after validation I think it’s easier to just use a custom base.php for those pages.