Load a specific blade template for custom URL structure with WP rewrite

Hey folks!
I’ve been enjoying Sage 10 since it launched, but I’ve come to a difficulty and the subject doesn’t seem to have been brought up already.

I’m working on a site where I need to use a specific blade template for a section of the site (tech specs of products) not related to any post type (singular) or common WP stuff (home, search, archive…) as I’m using tables already existing that were not created for Wordpress.

I’m using add_rewrite_rule() for the custom URL structure and to populate new query_vars, but then I don’t know how to load the blade template I need.

On a basic WP theme I would be creating a “specs.php” file at the root of the theme and load it from a “template_include” filter, but with Sage 10 I don’t know what the best practice would be.

So obviously, this does not work, as it is expecting a PHP file location and not a view:

add_action('template_include', function () {
    if ( get_query_var('view') == 'specs' ) {
        return view('specs');
    }
});

Or should I use a ThemeServiceProvider ?

Any ideas how to do that?

Thanks!

I don’t know if it’s the best way, but I ended up doing this and it works:

add_filter('template_include', function ($template) {
    if ( get_query_var('view') == 'specs' ) {
        return get_template_directory() . '/resources/views/specs.blade.php';
    }
    return $template;
});

Not sure what the best way is, but in my last project with Sage 10 I used custom routes with something like this: return locate_template( app('sage.finder')->locate('specs') )

2 Likes

Neat! It works.

Thanks!