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! ;)

iPhone 4, crazy for it or not?

Lineup

Everyone in Malaysia seems to be gone crazy with iPhone 4, like how it happen in the US. A long line with hours of waiting. Honestly, I’ve been waiting for a better Applee mobile phone, the 3G and 3Gs are just not a phone for me. They are more like an entertainment, chick-magnetic device instead. Check around, every iPhone users are talking about the latest apps they’ve installed, the softwares they’ve updated.

iPhnone 4

If I were to carry an Apple mobile phone, I would expect:
1, Worry free, like how Mac Computer advertised,
2, Not much of whatever updates, that include the application,
3, A full functional phone, including all neccessary features too.

Nokia E71

Currently I’m carrying a Nokia device, which is more than enough functions and features for me. From Word prosessing, PDF reader, camera, GPS, emailing, web browsing.

The only thing that may attract me to iPhone, perhaps is the design :P I like all Apple product design, but not necessary the product features itself.

Limit the search keywords in WordPress

If you are having a nich content, there are times you wanted to only let visitor search a few specific keywords, to boost the site keyword ranking or whatever reason. In default, WordPress come with a simple search form, with an input and a button. A lot of users find it quite useless.

While searching for some solution on my project, I find some trick on limiting the keyword, but using a dropdown selection menu.

<select name="s">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>

Search select
WordPress engine use the name="s" as its listener for keyword. Now that I am using manual selection, I can bascially place only keywords I want the visitor to search for. Well, unless the visitor is a WordPress user too, as there is always possible to manully type in /?s=keyword.

Hectic and sleepless

Isomnia coming back again, this time it is more than a week now. I don’t know how to solve this already. I’ve tried the solution I used when I was in college, but it doesn’t work well this time. With all the sleepless night, it has became the weight loss pills that work much better than exercise -2kg a week.

Theme Options

First of all, WordPress is seriously fun when playing with. Both of us has been playing and trying to come out some good options. From some simple settings to custom post types and custom fields, these are some of the tricks I’ve been spending time on.

Document

Doecuments – Our working desk started to have papers!!! Lots of papers, from previous construction project to now the accounting. I bought a new HP printer for those too. Learning account from bottom up again. What I’ve learn back in secondary school has all been forgotten. If only I’m hardworking back then.

Malaysia Day

Then I made some trip down the town for some fun together with all the people. I have always forgotten that I am a married man, and someone is there waiting for me to bring her around :P

Anyway, I need a bit of real sleep tonight, can’t wait for 4am sleeping hour again!

I am who I am

New YouTube video by Namewee arosed again. Only that, this time it seems more professional.

If this is one of the song in his album, I will definitely skip it. For one, I want to listen to his music, not his life. Even a made-believe-person life and much better than a musician life.

Skinny namewee

The video shows some of his early teen’s photos. Which is, so different, not just the hair. But the overall weight :P He definitely need to get some work out or check some diet pill reviews.

But listen carefully, it does has some beat like Jay Chow though. Are we seeing a Malaysia Jay Chow? Composer: Namewee, Lyrics: Namewee, Director: Namewee, Photography: CheeMeng, doesn’t it telling us something? :D