Using a Theme inside a Plugin directory

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