WordPress already includes a function which enables you to switch themes:
<?php switch_theme( $stylesheet ) ?>
The only problem is, this function will write to your database.
If you need to change themes conditionally, another solution is to use the stylesheet & template filters in WordPress which you can add to a simple plugin like this.
The above example will display the Twenty Fourteen parent theme to all logged out users and display whatever theme you have activated in the backend to all logged in users.
How To Use This Code
You’ll need to create a plugin with this code then install and activate it.
- Create a new file named plugin.php using a code editor like Notepad++ and copy the PHP code from the view raw link in the Gist labelled plugin.php.
- For the readme.txt file you can simply use notepad to create a new file named readme.txt and paste the text into the new file.
Then all you need to do is place both files in a new folder and send it to zip so you can upload, install & activate the plugin.
I’m curious as to why this would be necessary. Can you provide a sample use-case? I usually try to stick to back end stuff, but expanding my horizons is paramount.
Thanks,
Mark
Mark
Use different themes on different sized screens and mobile devices.
Also for different countries and languages and different layouts and colors based on any conditions.
Of course!
Thanks, Brad.