I need to redirect a custom template page to Homepage when a querystring key has an empty value.
for ex: https://example.com/customtemplatepage/?value=1 the Customtemplatepage is a page set with a custom template customtemplate.php in the root of the theme.
Whenever the querystring key "value" is empty it needs to be redirected to the root ("/" or homepage).
- I tried to catch that in functions.php with
add_action('wp_redirect','function');
, but it is too early asglobal $template;
is still empty / the customtemplate.php is not loaded yet - When I do it in customtemplate.php it is too late to use
wp_redirect();
as the headers are already out there
It's possible to use JS with window.location
in the customtemplate.php, but that's not an option as we have to do it server side.
The
template_include
filter should do the trick.Out of curiosity, why redirect to the homepage? Is a 404 not meant for handling non-existent content?