Theme Demo in live Site

Drop this inside a .php file in in wp-content/mu-plugins:

add_filter('template',   'check_for_theme_preview');
add_filter('stylesheet', 'check_for_theme_preview');

// if you want to display the theme selector
// (this is wrong, you need a custom action in each of your themes, after <body>)
add_action('wp_head',    'display_template_selector');


// checks for the "theme-preview" argument, and changes the template
function check_for_theme_preview($template){
  session_start();

  if(isset($_SESSION['template']))
    $template = $_SESSION['template'];

  // sanitize this!     
  if(isset($_GET['theme-preview']))
    $template = $_GET['theme-preview'];

  $_SESSION['template'] = $template;

  return $template;
}


// theme selector
function display_template_selector(){

  $themes = get_themes(); ?>

  <select onchange="document.location.href = this.options[this.selectedIndex].value;">
    <?php foreach($themes as $theme_name => $theme_data): ?>
    <option value="<?php echo add_query_arg('theme-preview', $theme_data['Template']) ?>"><?php echo $theme_name; ?></option>
    <?php endforeach; ?>
  </select>
  <?php

}

So pointing your browser to http://yoursite.com/?theme-preview=twentyten will make the site load that template, for the entire session.

But there are plugins out there that do this better…

Leave a Comment