wongpk

blog of a borneo web designer

Auto detect mobile style with WordPress

Mobile web and mobile web applications are both serious hit into the market nowadays. Even though Malaysia system is a bit slow, but the users are growing. With WordPress, there is not much you can do with mobile version, beside getting a plugin like WordPress mobile edition or a third party web app like mobify.

Mobile web

I am not keen with a list of plugins in my admin panel, so I found quite an useful snippet in a forum discussing how to automatic detect mobile browser and sent users to the mobile version . The snippet looks like this:

<?php

$mobile_browser = '0';

if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i',
    strtolower($_SERVER['HTTP_USER_AGENT']))){
    $mobile_browser++;
    }

if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or
    ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))){
    $mobile_browser++;
    }

$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda','xda-');

if(in_array($mobile_ua,$mobile_agents)){
    $mobile_browser++;
    }
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
    $mobile_browser++;
    }
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
    $mobile_browser=0;
    }

if($mobile_browser>0){
   header('Location: http://YourSite.mobi/mobile');
   } else {
   header('Location: http://YourSite.mobi/pc');
   }

?>

While it will automatic redirect users to the mobile version, it can however not direct users to another URL but another style sheet too.

if($mobile_browser>0){
   echo "<link rel=\"stylesheet\" href=\""; echo bloginfo('stylesheet_directory'); echo "/mobile.css\" type=\"text/css\" media=\"handheld\" />";
   } else {
   echo "<link rel=\"stylesheet\" href=\""; echo bloginfo('stylesheet_directory'); echo "/screen.css\" type=\"text/css\" media=\"handheld\" />";
   }

WordPress theme need style.css. All the information about the theme should be within style.css too. So I only place information on style.css and the rest of my CSS will bne in screen.css.

In functions.php, create the auto detect function:

function mobile_screen() {
$mobile_browser = '0';

if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i',
  strtolower($_SERVER['HTTP_USER_AGENT']))){
  $mobile_browser++;
  }

if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or
  ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))){
  $mobile_browser++;
  }

$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
  'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
  'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  'wapr','webc','winw','winw','xda','xda-');

if (in_array($mobile_ua,$mobile_agents)) {$mobile_browser++;}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'OperaMini')>0) {$mobile_browser=0;}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {$mobile_browser=0;}

if($mobile_browser>0){
  echo "<link rel=\"stylesheet\" href=\""; echo bloginfo('stylesheet_directory'); echo "/mobile.css\" type=\"text/css\" media=\"handheld\" />";
  } else {
  echo "<link rel=\"stylesheet\" href=\""; echo bloginfo('stylesheet_directory'); echo "/screen.css\" type=\"text/css\" media=\"screen\" />";
  }
}

It is to be easy for front-end developing. For header.php, you’ll need to look for the function and display it:

<?php if (function_exists('mobile_screen') ) { mobile_screen(); } ?>

Do let me know if there is another easier way to do so, cause this is not a good way to implement as a mobile web yet. By the way, you can learn more about mobile web tips and technique here. Have fun! ;)

3 comments on Auto detect mobile style with WordPress

  1. TimmyLicious
    September 30, 2010 at 9:25 am

    hmm ya that is cool. i used plugin for that….and one thing i really want is some wordpress apps for my Nokia E72. It would be easier to update blog and upload picture on my blog.

    It is still being develop and i tried it but failed. more info here. http://dev.nokia.wordpress.org/

    Reply
    • wong
      October 1, 2010 at 3:22 pm

      Whoot! Good one. But… That’s a lot of work there :lol:

  2. teguh
    December 7, 2010 at 11:48 pm

    I came for a while just 10 minutes, just read your post;)

    Reply

Leave a reply