How to use a theme inside a plugin directory? (5 posts)

  1. sxss
    Posted 1 year ago #

    Hello wp community!

    I try to write a little plugin for a mobile theme. The plugin should checks if the current users has a mobile device ( true == wp_is_mobile() ) and then uses a mobile-theme instead of the usual one.

    My first tought was to seperate the plugin and the theme, but this solution is not very user friendly. It's realy easy to overwrite the activated theme in case of a mobile device, but users had to download the plugin first, than the theme.

    Than i have tried to use use the template_redirect hook to include my theme file and use die() after that. It seems to work, but wordpress thinks i use the standard theme (twentyeleven) and loads this themes functions.php code into my mobile theme (include the css and skripts).

    Has anyone an idea how to set a theme directory outside of the /wp-content/themes/ directory?

    Here my two solutions: https://gist.github.com/sxss/a0b4c235d8dcfc8681e5

    Thanks for your help!

  2. bcworkz
    Posted 1 year ago #

    In my opinion, attempting to theme a WP installation as a plugin is misguided. It may be possible to hack WP into using files outside of /themes/ but doing so was never intended.

    Have you considered presenting your code as a child theme to whatever theme a user happens to be using? Then such path issues are handled almost automatically for you, you only need selectively override templates and styles as needed.

    The biggest issue I see with this is you need to have the user select their preferred parent theme and alter the child style.css to refer to it. This could be part of the activation process, so I don't think it's insurmountable.

    The other issue I know of is some commercial themes are actually child themes of a common base theme (i.e. theme forest). People using such themes could not use your child theme since you cannot create a grandchild theme.

    This is just my opinion, take what you like from it and do what you think is best.

  3. sxss
    Posted 1 year ago #

    Hello bcworkz and thanks for your answer.

    I have already thougt about a sepration of the plugin and themes, but then the users have to download and activate the plugin, after that download one of the mobile themes i want to provide and go back to the plugin settings to enable the theme. Usualy i would have disable the plugin, if it forces me to download and activate other things to do its work.

    Here some plugins which also have there themes build:

    Best regards

  4. bcworkz
    Posted 1 year ago #

    Just because it's been done doesn't make it a good approach ;)

    Even if you don't like my approach, I'll try to help you with yours. It may take some digging to capture all the functions and data that reflect where a theme resides. I believe they are all filtered, so it's probably doable, just not simple. Provided the alternate location is still under /wp-content/. If not, it may not be possible. Start with the global $wp_theme_directories. At least a couple references are in the options table, so filter 'option_stylesheet' and 'option_template' to start with. Some of the theme location functions may work just by changing those 3 things.

    Any that do not will need filtering on a case by case basis. I've never tried anything like this, but those items are certainly where to start. Good luck with your plugin mobile themes.

  5. sxss
    Posted 1 year ago #

    Thanks for your posting, i will check it out!

Topic Closed

This topic has been closed to new replies.

About this Topic