Switch themes based on query argument

If you like my WordPress work, check out my new plugin, Bylines. Thanks!

Jonathan asks:

> So for lack of a better explanation of “it will make my life easier in the short term” I’m in a situation where I’d love to be able to switch themes at runtime if viewing a single CPT entry. Do I need to be talked off a ledge before spending time researching the various theme switcher plugins out there and implementing such a thing?

WordPress looks at the `template` and `stylesheet` options to determine which theme to load. You can filter these at runtime based on a query argument.

Create a mu-plugin called `switch-themes.php` and place this inside:

“`
if ( ! empty( $_GET[‘theme’] ) ) {
foreach( array( ‘stylesheet’, ‘template’ ) as $option ) {
add_filter( “pre_option_{$option}”, function(){
return $_GET[‘theme’];
});
}
unset( $option );
}
“`

You should probably add a bit of security around this if you want to use it in a public environment.