Looks like all I needed to do was put the template_include
filter inside of the init
action. Source: https://stackoverflow.com/a/50037707/5749974
add_action( 'init', function () {
$product_base = ltrim( get_option( 'woocommerce_permalinks' )[ 'product_base' ], "https://wordpress.stackexchange.com/" );
add_rewrite_rule( "{$product_base}/([^/]+)/test", 'index.php?product=$matches[1]&test=1', 'top' );
add_filter( 'template_include', function( $template ) {
if ( get_query_var( 'test' ) ) {
$template = get_stylesheet_directory() . '/templates/test.php';
}
return $template;
} );
} );
Now https://example.com/products/my-product
and https://example.com/products/my-product/test
both load correctly!
Related Posts:
- add_rewrite_rule and template redirect not working
- Remove subcategory slug from url
- Getting 404 when using rewrite rule for posts
- How to redirect image attachment to its original post
- How to create custom URL routes?
- Post/Page Preview Template
- How to get Images included in Post
- Custom permalink structure only for default posts
- Preventing index.php?category_name=something from redirecting
- How to Group Posts by the First Letter or Number?
- Pretty URL with add_query_var
- get_permalink returns default link
- Custom taxonomies, with custom rewrites/slug, AND loading a taxonomy archive template from a plugin
- My ‘post attributes’ is missing from my WordPress install
- posts page – different lengths of excerpt
- add_rewrite_rule not working for page var
- Rewrite Rule for Custom Page with Query Vars in URL
- Forcing oembeds to top of post
- Accessing $post variable from template part
- Why does rewrite rule work for page not for custom post type post?
- how to show all post in my page-grid.php template page
- Use template of parent category for single post sub-categories
- loop not displaying posts on custom template page
- Including text at the end of every post
- custom single.php not working
- How can I create custom URL routes?
- showing posts of an specific category in admin custom menu with style of admin posts section
- $_GET parametters not working in an specific template
- Give posts a particular template based on the assigned category
- Add pagination to a template loaded by query variable
- Display a post via template
- Single post full screen template not inheriting WordPress role permission
- How to set a certain Custom Template to a Single Blog Post
- Post-Archive like a page in a specific subdirectory
- List all native posts in template page?
- Sidebar on single.php not showing up
- How to Featured content with images in home page?
- Post Templates and Post ID’s
- Custom Post Type, post-formats associated to custom post type > 404 archives
- How do I disable the “by author” hyperlink on posts?
- Trying to query all posts in category 4
- Output Buffer Issue with Single Post View
- Prevent 404 of Author pages without posts
- Custom post type and taxonomy page templates
- How to save template data into wp_post table (post_content column)
- Add static text to post URLs
- How do I change the template specifically for single posts?
- Related post not showing table of content
- My post permalinks are redirecting to the post archive page for some reason?
- Post not using taxonomy template
- Why does my “Add New Post” page have an old post in it?
- Customizing a new page used for blog posts with Avada theme
- How to add a ‘News’ section to specific posts in WordPress
- Add a url parameter to the current post and read it in a shortcode
- Prevent Archive URLs
- Single Sidebar; multiple templates! How?
- Slug collision between page, taxonomy and custom post type
- How to Display Most View Post in the template file?
- How to add category base prefix to wordpress permalink
- Use special template for the first post in the loop
- Is it possible to query a category with specified posts?
- Pretty Filter URL
- Custom post type template not called
- Page 2 has no posts?
- Multipage Post URL correction
- single- and archive- templates not being applied to custom post types, not even after flushing permalinks
- Fixing the WP Post Object for Custom Route
- How to change post template via url?
- how to remove entry meta from wordpress archive category and change its display
- My blog “page” used to show a list of posts, but now it’s just showing the page content?
- Posts in Page – How to customize the URL?
- Custom template for different post types
- Use a custom post URL that is outside the blog root directory
- Adding The Post Template Dropdown to WP-Admin
- Posts page template: How can I edit the markup for this?
- How to arrange different post categories in template
- Rewrite a specific Post Format to URL slug
- How can I change the look of a post as is displayed
- Which hooks are essential for post templates?
- Overwrite URL on blog posts
- Display the first post’s comments of category in comments.php template
- 1 post, 2 templates
- how to style an individual page in a category [duplicate]
- How to display a certain template element only for posts published within certain time range?
- Redirecting posts in WordPress to a specific page
- Creating / Editing a post from within site template
- custom post type single page template not working
- WordPress “Posts Page” isn’t showing template dropdown
- How do I create a template page to show 3 blog posts?
- How to add a new page that will function as a separate blog/news article page
- excerpt in template for specific page
- Tag page that displays specific posts
- Is there a block to print post link standalone in a block theme?
- Add additional URL variations for a Post
- Blog posts are not appearing as subs of Blog Page
- How can I locate the single buddypress forum post template? [closed]
- How to change url of posts?
- Post pages are redirecting to the 404 page in wordpress
- Display A Post At A URL without Redirecting
- `query_var` values empty in theme file after `add_rewrite_rule` redirection