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?

Read More
  /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.

Related posts

Leave a Reply

4 comments

  1. 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.

  2. 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.

  3. 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';
    
     }
    
    dd_filter('stylesheet','loading_mobv_style');
    add_filter('template','loading_mobv_template'); 
    

    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 🙂