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
- Forcing oembeds to top of post
- Why does rewrite rule work for page not for custom post type post?
- Including text at the end of every post
- custom single.php not working
- Adding variable for post title so post template can include auto-generated link to view on external website.com/{post title}
- Change Theme from Plugin based on query_vars
- How only display all post related to category
- WordPress not respecting template hierarchy (fetches index.php instead of single.php or page.php)
- Display page content AFTER a loop of posts
- Single post as homepage?
- URL Rewriting if it is a single post / redirecting post – post type
- Category Template: Need to display different content on first page of archives
- Change post_date to post_modified date on post template?
- add_rewrite_rules for type of post
- Custom rewrite_rules – only pass numbers and not alphabetic characters
- Changing Permalinks for Default Post type to reflect navigation
- How can I pass the ID of a post to a page template by clicking a url?
- Using custom post template
- What is the filter or hook to add admin controls to posts on the front end?
- Rewrite Rules to custom template
- Custom taxonomy rewrite with query var returns %2F in URL
- Switch to page template when using post permalink
- Genesis + Ajax + Jquery | Failling to call action
- Get Page ID of blog homepage
- URL rewriting with custom user meta “/%shop_name%/gallery/%gallery%”
- Visting slug for ‘post’ post type shows 404 not archive?
- Send post id through ajax and get the post content back
- custom post template file not shown, instead all the time 404.php
- Custom Post Type Rewrite To Include Parent Page(s)
- How to create a template for Pages?
- Disable WordPress Archive Conflict Check
- Hide a specific post from a page template
- Tell WP to use a specific template file in posts
- How to make different custom post layouts?
- Is it possible to give the user the chance to select between two single template files to use in a post?
- how to set social icons to product / post template (Auros theme)? [closed]
- How to identify and hide/remove an element from a WordPress page [closed]
- wordpress posts template remove default template from menu
- 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
- 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?
- How do I disable the “by author” hyperlink on posts?
- Add static text to post URLs
- How do I change the template specifically for single posts?
- My post permalinks are redirecting to the post archive page for some reason?
- Post not using taxonomy template
- Customizing a new page used for blog posts with Avada theme
- Prevent Archive URLs
- How to Display Most View Post in the template file?
- Use special template for the first post in the loop
- Is it possible to query a category with specified posts?
- Pretty Filter URL
- 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
- Posts page template: How can I edit the markup for this?
- Which hooks are essential for post templates?
- Overwrite URL on blog posts
- Display the first post’s comments of category in comments.php template
- 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
- custom post type single page template not working
- How to add a new page that will function as a separate blog/news article 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 to change url of posts?
- Post pages are redirecting to the 404 page in wordpress