Using a Theme inside a Plugin directory

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?

  /wp-content/plugins/mobview/theme/

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.

4 Answers
4

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:

<?php
/*
Plugin Name: Mobile View Plugin
*/

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

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.

Leave a Comment