Post Pic

How to detect mobile visitors on your WordPress blog

As mobile devices, as such as Blackberries or iPhones, are becomming more and more popular, bloggers may want to detect those visitors and redirect them to a mobile version of their blog. Here is a recipe to detect mobile visitors.

To achieve this recipe, you first have to get the code from detectmobilebrowsers.mobi and upload it to your theme directory.

Once done, simply open your header.php file and place the following at the top of the file. Don't forget to edit line 5 according to the page where you'd like to redirect mobile users.

include('mobile_device_detect.php');
$mobile = mobile_device_detect();

if ($mobile==true) {
  header( 'Location: http://your-website.com/?theme=Your_Mobile_Theme' ) ;
}

Thanks to Jeff Starr for this very cool idea!

By the way, did you checked out the Headway theme for WordPress? It is pretty amazing. I'll publish a review about it soon at Cats Who Blog.

Related Posts

Related Posts

No related posts.

16 Responses

Dec 09 2009 15:21

Nice tips, I will try it later, thanks for sharing.

Dec 09 2009 15:37

Funny I found this as I just started doing a similar thing yesterday for one of my clients

Dec 09 2009 16:48

Very Cool,, Thanks for sharing.

Dec 09 2009 17:07

I think a few friends have tried using my blog on there mobile just to see what it does. It doesn’t work how I want it too. This is a great tip.

Dec 10 2009 17:34

Please don’t just redirect to your homepage, since this will throw off mobile users who are linked to a specific post. Many sites do this, and it frustrates me to no end!

Dan Smart over on digwp.com has a much better solution:

function filter_switchTheme($theme)
{
$mobile = mobile_device_detect();
if ($mobile==true)
{
$theme = 'mobile_theme_name';
}
return $theme;
}

And here’s the filters to call:

add_filter('template', filter_switchTheme);
add_filter('option_template', filter_switchTheme);
add_filter('option_stylesheet', filter_switchTheme);

Dec 10 2009 17:41

I will try it out on one of my blogs. Thanks

This is a great tip, I’ll definitely give it a try.
Say, does it only work for Blackberries and iPhones or does it also work for other mobile phones with internet?

Dec 10 2009 23:36

@Author: nice sharing,, but …
@Trevor: i agreed with you

Dec 14 2009 17:05

Even better is the beautiful wptouch. Check it.

Dec 17 2009 22:24

thanks i will share it to my readers, of course i will link to you.

Jun 23 2010 10:49

I could nt see how this would know the different types of mobile browser but if you look at the detail code on detectmobilebrowsers.mobi it starts to make sense. It has a test for each type of broswer and you can set it to true if you want to treat that browser as mobile. Neat.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required