WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am building a mobile friendly plugin and put the theme directory inside the plugin directory.

If it's a mobile browser, how can I redirect to the theme in the plugin directory?


I've managed to use the following redirection:

wp_redirect( plugins_url('/mobview/theme/index.php') );
exit ;

But am kind of lost in the directory redirect inside WordPress structure.

share|improve this question
up vote 3 down vote accepted

Hi @Hamza:

I think what you are looking to do is for your plugin to hook 'template_include' to tell it to load a file from your plugin directory. Here's starter code for your plugin:

Plugin Name: Mobile View Plugin

if (is_mobile_user()) // YOU NEED TO DEFINE THIS FUNCTION
  class MobileViewPlugin {
    static function on_load() {
    function template_include($template_file) {
      return dirname( __FILE__) . '/theme/index.php';

Of course this will require that you somehow filter out when it is not a mobile user by defining the is_mobile_user() function (or similar) and it also means that your /theme/index.php will need to handle everything, included all URLs as you've basically bypassed the default URL routing by doing this (or your could inspect the values of template_file and reuse the logic by routing to equivalent files in your plugin directory.) Good luck.

P.S. This does not provide a mobile solution for the admin. That would be 10x more involved.

share|improve this answer
Wow , this actually the 3rd solution works , Thank YOU :D – Hamza Dec 27 '10 at 21:19
@Hamza - Glad I could help. (If it is the one that works would you mind if I ask you to select it as the accepted answer? Or at least select one of the answers. Thanks.) – MikeSchinkel Dec 28 '10 at 0:17
welcome , sure will select it .. sorry for late got stuck again at work :( – Hamza Dec 30 '10 at 10:39
@Hamza - Glad it helped! – MikeSchinkel Dec 30 '10 at 23:29

You misunderstand mechanics of how theme works. It does have index.php template, but request is still processed by WordPress main index.php in site's root. It makes no sense to redirect visitor to theme's folder because themes are not meant to be visited directly (they expect WP core to be loaded for them and such).

I am not proficient with such plugins, but I imagine they are likely using template_redirect hook to load their templates instead of those of active theme on match to mobile browser.

I think they are quite a few plugins on this topic in official repository, you can look through their code to get an ideas for common techniques used.

share|improve this answer
i agree with you – ntechi Dec 27 '10 at 11:54

if i am not wrong then try this code

$includes_path = TEMPLATEPATH . '/your folder name/';

then write this

require_once ($includes_path . 'file name');

share|improve this answer
didn't work !!! , i should add this an action to wp_header or where exactly :( .... i added it in /pulgins/mobview/mobview.php , the wordpress now displaying a blank page ! – Hamza Dec 27 '10 at 11:18
the first code should be written on the very first line of your mobview.php and the secod line, when you require that theme file – ntechi Dec 27 '10 at 11:29
hmm ,all i am thinking of now , is when to activate the plugin it will use the theme used in : /wp-content/plugins/mobview/theme/ ... i have red about register_theme_directory but i don't think this will do it . – Hamza Dec 27 '10 at 11:39
did that , didn't work ! – Hamza Dec 27 '10 at 11:44
what you want actually? the files you require? rite? – ntechi Dec 27 '10 at 11:47

well ,

i think , i mis-explained my question , thu i explained more in the sub-comments :

All i needed is : to activate another theme , in different directory , and to do such thing

first :

register the new theme directory :

register_theme_directory( WP_PLUGIN_DIR . '/mobview/theme' );

2nd :

to add a filter to use this theme :

function loading_mobv_template() {
    return 'hamzamob';

function loading_mobv_style() {
    return 'hamzamob';



Now all i need to do is to select if the browser is a mobile or not then activate the action .

Thank you .

update : this is the answer to my first question up . Thank you :)

share|improve this answer
@Hamza - Are you answering your own question or just clarifying your question? If you are clarifying your question please delete this answer and edit your original question to include this. – MikeSchinkel Dec 27 '10 at 13:33
@hamza why dont you ask another question where you have actual doubts, here your question is not proper, try to frame it and ask again, and answer a new question everytime instead edit your original question – ntechi Dec 27 '10 at 14:02
@MikeSchinkel : usually i ask a question and keep searching till i find it , this time i found what am looking for then posted it in here , is this against the rules - thank you :) – Hamza Dec 27 '10 at 14:06
@ntechi : i don't have another question yet and i believe i made myself clear in the first one , sorry for editing the first question to put the code in it .. thank you :) – Hamza Dec 27 '10 at 14:07
@hamza : i am not saying that your question is different, i just want to say that second question you have to frame it, so that we all can understand it, coz your second question is probably your answer to your first question – ntechi Dec 27 '10 at 14:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.