WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there some way to change the page template dynamically using just a url I wanna use it in a demo theme, so for example when the user enters a particular URL ending with for example


it changes the template on the fly?

Could this technique be used to change the theme (or maybe a child theme) on the fly just for demo purpose?

share|improve this question
up vote 0 down vote accepted

Filter pre_option_stylesheet. Sample code, not tested:

add_filter( 'pre_option_stylesheet', 'wpse_100854_switch_stylesheet' );

function wpse_100854_switch_stylesheet( $false )
    if ( empty ( $_GET['theme'] )
        return $false;

    $themes = wp_get_themes( array( 'allowed' => true ) );

    if ( isset ( $themes[ $_GET['theme'] ] ) )
        return $_GET['theme'];

    return $false;

The URL should look like example.com/?theme=theme_directory_slug.

To change just the template for a specific page, hook into template_include:

add_filter( 'template_include', 'wpse_100854_switch_template' );

function wpse_100854_switch_template( $template )
    if ( empty ( $_GET['template'] )
            return $false;

    // do not allow to break out of the directory
    $new  = ltrim( $_GET['template'], '.' ); 
    $path = get_template_directory() . "/templates/$new.php";

    if ( file_exists( $path ) )
        return $path;

    return $template;

Now add a directory templates to your theme. When someone requests example.com/?template=blue and the template blue.php exists in the special directory, it will be used.

share|improve this answer
Thanks for mentioning the function, I have found these links, could be useful: codex.wordpress.org/Plugin_API/Filter_Reference/… shibashake.com/wordpress-theme/switch_theme-vs-theme-switching – Hugh Man May 27 '13 at 10:28
Now, I have used this function, How can I use it? - I mean now what is the URL I am after? – Hugh Man May 27 '13 at 10:40
@CapitalH.ir See my update. – toscho May 27 '13 at 10:42
Why do you use 'pre_option_stylesheet' instead of 'pre_option_template'? Also, could you not use the action 'template_redirect'? – user27457 May 27 '13 at 10:47
@MagentaCuda This filter runs as early as possible. template_redirect is way too late. – toscho May 27 '13 at 10:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.