Skip to content

Instantly share code, notes, and snippets.

@itsananderson
Created June 24, 2012 08:02
Show Gist options
  • Save itsananderson/2982374 to your computer and use it in GitHub Desktop.
Save itsananderson/2982374 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