Dec 30, 2009

How to detect mobile browsers & Redirect mobile users to another URL

Mobile detection is not difficult to implement on WordPress, just grab any of these plugins and plug it in. Or you can try these mobile version creation tutorials. You get a mobile theme right away. Many of them are awesome too. In fact, I’ve tried more than one and quite honestly, they are really good. The only problem you encounter if at all, would be compatibility issues with other plugins such as the cache plugins.

And also some plugins do not detect all the browser agents, can’t really say that’s a problem but, you know… I’ve tried the mobile Wordpress themes as well and found that sometimes, things can get quite out of hand, like when I tried browsing from Opera, it gave me the mobile version, now that’s something you don’t want to happen with your visitors.

So, here’s an easy work around I found, with help from Lijeesh.
Instead of switching to a mobile theme, I thought why not do something simpler, and of course more reliable ?

The easiest way I found is to redirect mobile visitors to you Google blog reader feed. Amit has a “trick” here, where he explains how you can append the feed reader URL, and get a mobile version of your feeds. After this, using a mobile browser detection script, you can easily redirect the users to your “mobile page”. Here’s how. (Amit does not explain how to automatically redirect mobile users, but this one is a “install and forget” kind of a work around)

Step 1
Download the Mobile browser detection script from here.

Step 2
Upload the script to your theme folder
That would be yourdomain/wp-content/themes/yourthemefolder/

Step 3
Go to your Theme Editor, and then functions.php

Step 4
In functions.php, soon after the php opening tag, add this code –
Make sure you change the feed URL (in my case – to match your blogs. That is after the feed/ portion. That’s it, now close and check via a mobile phone.

I realize that there are lot of work arounds available to achieve the same but I found this amazingly simple. Or may be its because I like the idea of redirecting mobile users to the Google reader version effective, because while you do it, you’re actually making it really simple.

- Users can easily subscribe via Google reader
- Your AdSense ads are not missed out (some mobile themes skip it)
- Everything you put into your RSS feeds, show up on the mobile version as well
- No theme glitches, downtimes or screw ups (Unless Google scres up, which is very unlikely)


Post a Comment