I have a query var show=othertemplate
that I can add to any URL to show any post, page or archive in Wordpress in a different template, like this: mysite.com/[any URL]/?show=othertemplate. I'd like for it to be accessible via clean URL, like [any URL]/othertemplate or [any URL]/show/othertemplate.
I have tried achieving this by registering the query var with a query_vars filter and adding code like below to .htaccess, but Wordpress intervene and throws a 404. WP Debug Bar says that the query is looking for attachment=print.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)/othertemplate/?$ /index.php/$1?view=othertemplate [L]
I have also tried using the Wordpress rewrite functions, but the same thing happens.
function st_myqueryvars($vars) {
$vars[] = 'show';
return $vars;
}
add_filter('query_vars', 'st_myqueryvars');
function st_myrewriterules( $wp_rewrite ) {
$newrules = array();
$new_rules['^(.*)/othertemplate/?$'] = 'index.php/$matches[1]?show=othertemplate';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules','st_myrewriterules');
Is there something I'm missing here?
Update: After flushing permalinks I see that this WP function rewrites /[any URL]/template to index.php?show=template instead of index.php?[the URL's corresponding query string]&show=template. I can't 'hard code' a query string like post_type=$matches[1]&name=$matches[2]... into the rewrite rule because it needs to work for any page/post/archive, which have different permalink structures. How do I get around this?