I am setting the page template for a route but index.php always follows the custom template, is it because it's 'include_'? Feels like it should be 'set_' or 'load_'... I want to replace the output with my template, not just include my template in the output.
add_action('template_include', [$self, 'load_route_template']);
public function load_route_template($template)
{
if (!$this->matched_route instanceof Route || !$this->matched_route->has_template()) {
return $template;
}
// No file found yet
$located = false;
// Continue if template is empty
if ( empty( $this->matched_route->get_template() ) )
return;
// Check child theme first
if ( file_exists( trailingslashit( get_stylesheet_directory() ) . 'rcp/' . $this->matched_route->get_template().'.php' ) ) {
$located = trailingslashit( get_stylesheet_directory() ) . 'rcp/' . $this->matched_route->get_template().'.php';
// Check parent theme next
} elseif ( file_exists( trailingslashit( get_template_directory() ) . 'rcp/' . $this->matched_route->get_template().'.php' ) ) {
$located = trailingslashit( get_template_directory() ) . 'rcp/' . $this->matched_route->get_template().'.php';
// check plugin directory
} elseif ( file_exists( plugin_dir_path( dirname( __FILE__ ) ) . 'public/' . $this->matched_route->get_template().'.php' ) ) {
$located = plugin_dir_path( dirname( __FILE__ ) ) . 'public/' . $this->matched_route->get_template().'.php';
}
if ( ! empty( $located ) )
load_template( $located );
if (!empty($route_template))
$template = $located;
return $template;
}
The code I'm using is based on this tutorial, and the routing works great, the template is loaded when I hit certain routes but the index.php always follows it.