0

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.

1 Answer 1

1

template_include is a filter hook so you should use add_filter() instead of add_action(), it's breaking the semantics here. Though both functions internally do the same thing.

Try the following code, I haven't tested it but it should work. Simplified and removed some codes.

add_filter('template_include', [$self, 'load_route_template']);
public function load_route_template($template)
    {
        if (!$this->matched_route instanceof Route || !$this->matched_route->has_template() || empty( $this->matched_route->get_template() ) ) {
            return $template;
        }

        // No file found yet
        $located = false;

        // 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 ( $located ) {
            $template = $located;
        }

        return $template;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.