Need to switch theme when accessed from sub domain. There is nothing installed in the sub domain. I have setup a subdomain as a CNAME pointing to the original domain. Then, detect whether or not the visitor is on the subdomain by looking at $_SERVER[‘SERVER_NAME’]. Went through various sources and have found out this code but could not make this working.

Note: I am a newbie

// regular site url
$my_url = 'http://example.com';

// subdomain you want a different theme on
$my_sub_url = 'http://sub.example.com';

// subdomain prefix, used to test whether primary or subdomain
$my_prefix = 'sub.';

// folder name of theme to use instead on subdomain
$my_theme_name = 'my_mobile_theme';

add_filter( 'template'          , 'my_change_theme' );
add_filter( 'option_template'   , 'my_change_theme' );
add_filter( 'option_stylesheet' , 'my_change_theme' );

// these 2 actions rewrite all urls in the body
// from main to sub domain
add_action( 'wp_head'           , 'my_buffer_start' );
add_action( 'wp_footer'         , 'my_buffer_end' );

// TRUE, if this is the subdomain, false otherwise
public function my_is_subdomain() {
  global $my_prefix;
  if( strpos( $_SERVER['SERVER_NAME'], $my_prefix ) !== FALSE ) {
    return TRUE;
  return FALSE;

// if this is the subdomain, return new theme
// otherwise, return original theme
public function my_change_theme( $theme ) {
  global $my_theme_name;
  if( my_is_subdomain() ) {
    return $my_theme_name;
  return $theme;

public function my_buffer_start() {

public function my_buffer_end() {

// replace primary url with subdomain url in body
// so that all links keep user on the subdomain
public function my_buffer_cb( $buffer ) {
  global $my__url, $my_sub_url;
  if( ! my_is_subdomain() ) {
    return $buffer;

  // replace main domain with sub domain
  $buffer = str_replace( $my_url, $my_sub_url, $buffer);

  // !!! NOTE - you may not want to replace EVERY instance
  // for example, you may want to keep social media urls
  // intact, or rel="canonical", or links specifically
  // designed to switch between the primary and subdomain

  return $buffer;

WordPress supports only one theme on site. Actions like shown will cause a adverse effects. Instead, you should use WordPress Multisite, which allows to use different themes on subdomains. This is the way recommended by the WordPress team.

There is solution for WooCommerce on WordPress Multisite: WooCommerce Multistore plugin: It is quite expensive ($200), but struggling with unsupported features in WordPress can cost much more.

  • I have a woocommerce site, I wish to have a separate theme for mobile (web) and mobile app (android webview app). Multisite site does not sync woocommerce data with the mainsite which makes it unusable. Kindly let me know your suggestions. – venkat guru Jan 18 '17 at 2:41
  • I have edited my answer in accordance with new info about WooCommerce. – KAGG Design Jan 18 '17 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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