Mobile might not be a great example — responsive design would take care of that.
Were this my project I would take a slightly different approach. Instead of prefixing the URL, I would stick the mobile on the end with an rewrite endpoint. An endpoint combined with using the template_include
filter would work.
So you end up with a permalink structure like this
/{post_type}/{post_slug}
/{post_type}/{post_slug}/mobile
There are many answers that explain this elsewhere. For the mobile
endpoint.
<?php
add_action('init', 'wpse74553_add_endpoints');
function wpse74553_add_endpoints()
{
// EP_ALL adds the endpoint to everything.
add_rewrite_endpoint('mobile', EP_ALL);
}
// this makes the naked `mobile` endpoint work vs. having to
// use something `mobile/works`.
add_filter('request', 'wpse74553_filter_request');
function wpse74553_filter_request( $vars )
{
if(isset($vars['mobile']))
$vars['mobile'] = true;
return $vars;
}
From there you can hook into template_include
filter and change the template if the mobile
query variable is set.
<?php
add_filter('template_include', 'wpse74553_include');
function wpse74553_include($t)
{
if(get_query_var('mobile'))
{
// you have a request with the mobile endpoint
$t="mobile-template.php";
}
return $t;
}
The only thing to watch out for is that template_include
fires on every request. It’s not specific — so if you want to use it, you’ll probably need to do more checking than above. There’s also a bunch of more specific filters; take a look in wp-includes/template.php
to get an idea, specifically the function get_query_template
.
For single posts you could use the single_template
filter, for instance.
In short, you’re not at all limited by the template hierarchy, you can change it at will with the template_include
or any of the {{type}}_template
filters.