Can I obtain differents links for different installed theme?

I’ve done for this myself using the plug-in theme switcher: http://wordpress.org/plugins/theme-switcher/ (yes, it hasn’t been edited in 2+ years).

These theme stores a cookie in the browser indicating which theme the user has selected (themes are displayed as a drop-down / link list widget – and selecting a theme changes the cookie and redirects you to the home page).

You can dynamically change the theme with the stylesheet filter:

function wpse109022_change_theme_name( $stylesheet ){
    //Change stylesheet to appropriate theme (based on cookiee for example)
    //$stylesheet should be the theme 'name', e.g. 'twentytwelve'
    return $stylesheet;
}
add_filter( 'stylesheet', 'wpse109022_change_theme_name' );

Please note Theme Switcher also uses the template filter, to do the same thing. I believe this is for backwards compatibility. (2.5??)

//add_filter( 'template', 'wpse109022_change_theme_name' );

See source of theme switcher for more details


Caveat: one problem is that WordPress doesn’t remember templates between themes. For example:

  • Create page and choose template A from your theme
  • Change theme
  • Select template from the new theme and re-save the page
  • Switch back to original theme. The page no longer has template A

If you’re using page templates you’ll want WordPress to remember which template to use for which theme. I wrote a plug-in for this: http://wordpress.org/plugins/remember-my-template/

It essentially does the following:

  • Every time a page is saved, store the template as meta data with key _wp_page_template_{theme-name}
  • Filter the page’s template via get_post_metadata filter and replace it with the appropriate template based on the theme.

Leave a Comment