Change template in WordPress plugin

I want to detect if the subdomain is “m.”, and if so display a different template. Couldn’t find what I was looking for, in the WordPress API to do so.

Related posts

Leave a Reply

3 comments

  1. You can use the parse_url() function in PHP to do this:

    $parsed = parse_url($_SERVER['HTTP_HOST']);
    $host = explode('.', $parsedUrl['host']);
    $subdomain = $host[0];
    

    Then, you can add your own if statement:

    if ($subdomain == "m") { ... } else { ... }
    
  2. I suggest to create you own plugin with one single script and put it in wp-content/plugins with a code like this:

    <?php
    /**
    * Plugin Name: Theme swithwer
    * Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
    * Description: Custom theme switcher
    * Version: 0.1
    * Author: XXX XXX
    * Author URI: http://URI_Of_The_Plugin_Author
    * License: A "Slug" license name e.g. GPL2
    */
    
    add_filter( 'stylesheet', 'switch_ma_theme' );
    add_filter( 'template',   'switch_ma_theme' );
    
    function switch_ma_theme()
    
    {
       global $wpdb;
       // condition to test ad theme to load
       if ($_SERVER['HTTP_HOST'] == 'blog.xxxx.xxx')
           return 'xxxthemexxx';
       // else default theme
       $theme = $wpdb->get_results( "SELECT option_value FROM wp_options where    option_name='template' ");
       $theme = array_pop($theme);
       return $theme->option_value;
       // Return the theme directory name
    
    }
    

    Then go in the admin , look for you new plugin and enable it