Advertisement
miriamdepaula

WordPress: If is mobile domain, activates mobile theme

Jun 24th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * This solution assumes you've already set up your site so that the site domain is
  5.  * your "normal" (non-mobile) domain, and your theme is your non-mobile theme.
  6.  *
  7.  * In short, what it does it check to see if the site is being accessed through the
  8.  * mobile domain. If it is, the mobile theme is used instead of the normal theme, and
  9.  * all links point to the mobile domain (so navigatiion doesn't take visitors to the
  10.  * regular domain.
  11.  */
  12.  
  13.  
  14. define( 'MOBILE_DOMAIN', 'm.mysite.com' ); // Whatever your mobile domain should be
  15.  
  16. if ( MOBILE_DOMAIN == $_SERVER['HTTP_HOST'] ) {
  17.     // Override option table values for home_url and site_url
  18.     // This way mobile users will stay on the mobile domain
  19.     define( 'WP_HOME', 'http://' . MOBILE_DOMAIN . '/' );
  20.     define( 'WP_SITEURL', 'http://' . MOBILE_DOMAIN . '/' );
  21.    
  22.     // Set up a filter to override the site stylesheet/theme for this request
  23.     add_filter( 'pre_option_template', 'mysite_set_theme' );
  24.     add_filter( 'pre_option_stylesheet', 'mysite_set_theme' );
  25. }
  26.  
  27. // Override the regular theme with the mobile one
  28. function mysite_set_theme() {
  29.     return 'mytheme_mobile';
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement