Can anyone help me figure out how to do an internal redirect, using the template-wrapper?
My scenario: I have an angular app with ui-router running on ‘/category/goats-know/’. I’d like to turn on html5mode, which changes the Angular app URLS from ‘/category/goats-know/#/why-goats/’ to ‘/category/goats-know/why-goats/’. Which causes WP to 404, because such a page doesn’t really exist.
I have base-category-goats-know.php
, and category-goats-know.php
files
So somewhere, I want to run this code:
if (preg_match('/\/category\/goats-know\//', $_SERVER["REQUEST_URI"]) {
// load category-goats-know.php inside base-category-goats-know.php
} else {
// nothing to see here, carry on.
}
I’ve tried hooking into the sage/wrap_base
filter in lib/extras.php:
add_filter('sage/wrap_base', __NAMESPACE__ . '\sage_wrap_base_goats_know'); // Add our function to the sage/wrap_base filter
function sage_wrap_base_goats_know($templates) {
if (preg_match('/\/category\/goats-know\//i', $_SERVER['REQUEST_URI'])) {
array_unshift($templates, 'base-category-goats-know.php'); // Shift the template to the front of the array
}
return $templates; // Return our modified array with base-$cpt.php at the front of the queue
}
This gets me half way there, rendering the 404.php
template inside base-category-goats-know.php
.
So I tried using WP’s filter template_include
:
add_filter( 'template_include', __NAMESPACE__ . '\goats_know_template', 110 );
function goats_know_template( $template ) {
global $wp_query;
if (preg_match('/\/category\/goats-know\//i', $_SERVER['REQUEST_URI'])) {
if ($wp_query->is_404) {
$wp_query->is_404 = false;
}
$new_template = locate_template( array( 'category-goats-know.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
This also half-works, it renders category-goats-know.php
, but without the base template. I’ve tried it with priority of 99 and 110 with the same result.
I don’t understand enough of PHP OOP to figure out what I need from lib/wrapper.php
alone, would sure appreciate some help from someone who knows what’s going on there…