Skip to content

Instantly share code, notes, and snippets.

@blogjunkie
Forked from marco-s/mobile.php
Created November 23, 2016 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blogjunkie/03190adfed01b4daca249e7e8ab3a84a to your computer and use it in GitHub Desktop.
Save blogjunkie/03190adfed01b4daca249e7e8ab3a84a to your computer and use it in GitHub Desktop.
Switch between two themes, depending on what domain is used to access a site
<?php
/*
* This solution assumes you've already set up your site so that the site domain is
* your "normal" (non-mobile) domain, and your theme is your non-mobile theme.
*
* In short, what it does it check to see if the site is being accessed through the
* mobile domain. If it is, the mobile theme is used instead of the normal theme, and
* all links point to the mobile domain (so navigatiion doesn't take visitors to the
* regular domain.
*/
define( 'MOBILE_DOMAIN', 'm.mysite.com' ); // Whatever your mobile domain should be
if ( MOBILE_DOMAIN == $_SERVER['HTTP_HOST'] ) {
// Override option table values for home_url and site_url
// This way mobile users will stay on the mobile domain
define( 'WP_HOME', 'http://' . MOBILE_DOMAIN . '/' );
define( 'WP_SITEURL', 'http://' . MOBILE_DOMAIN . '/' );
// Set up a filter to override the site stylesheet/theme for this request
add_filter( 'pre_option_template', 'mysite_set_theme' );
add_filter( 'pre_option_stylesheet', 'mysite_set_theme' );
}
// Override the regular theme with the mobile one
function mysite_set_theme() {
return 'mytheme_mobile';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment