WP Mobile as well as WP PDA plugins for WordPress are great. They make your blog become available for mobile users without hassle.

However when you are using WP-Cache plugin also in the same time then there is a problem. That is your mobile visitors may be served with the cached pages of non-mobile ones, with all the bells and whistles. And similarly your non-mobile visitors may get the cached pages of mobile ones which are just too simple.

So what can we do? We don’t want to disable cache.. and we don’t want to give up mobile version as well.. :)

Here is the solution I’m using in this site.

As stated in their site WP-Cache has two phases Wordpress hooks. The first is called at the very begining wp-cache-phase1.php when just few code has been compiled. The second wp-cache-phase2.php after all plugins have been executed. The first phase checks if the requested URL is already cached, if so it serves from the static file and finishes. The second phase stores the generated page in a static file for future requests.

We can hack a little bit in the first phase code which is called at the very beginning. This code is stored in a file called wp-cache-phase1.php which contains the interesting lines at the beginning of file, that is..

if (!$cache_enabled || $_SERVER["REQUEST_METHOD"] == 'POST')
return;

There is a variable $cache_enabled that we can set to either true or false to allow or disallow execution of the rest of caching process. Therefore we can add a little code here to check the browser’s user agent, and set $cache_enabled to false if the browser is a mobile version or leave it alone otherwise.

$small_browsers = array(
	'2.0 MMP'
	,'240x320'
	,'AvantGo'
	,'BlackBerry'
	,'Blazer'
	,'Cellphone'
	,'Danger'
	,'DoCoMo'
	,'Elaine/3.0'
	,'EudoraWeb'
	,'hiptop'
	,'MMEF20'
	,'MOT-V'
	,'NetFront'
	,'Newt'
	,'Nokia'
	,'Opera Mini'
	,'Palm'
	,'portalmmm'
	,'Proxinet'
	,'ProxiNet'
	,'SHARP-TQ-GX10'
	,'Small'
	,'SonyEricsson'
	,'Symbian OS'
	,'SymbianOS'
	,'TS21i-10'
	,'UP.Browser'
	,'UP.Link'
	,'Windows CE'
	,'WinWAP'
);
foreach ($small_browsers as $browser) {
	if (strstr($_SERVER["HTTP_USER_AGENT"], $browser) !== false) {
		$cache_enabled = false;
		break;
	}
}

if (!$cache_enabled || $_SERVER["REQUEST_METHOD"] == 'POST') 
	return;

That is all. Upload your hacked wp-cache-phase1.php back to its plugin directory and now you can enable both of WP-Cache as well as WP-Mobile or WP-PDA plugins. Non-mobile visitors will be served using the cached pages as usual while mobile visitors will always get the fresh pages.

- My News Wire . net -

Tags: , ,

[?]
Share This

Description: WP Mobile as well as WP PDA plugins for WordPress are great. They make your blog become available for mobile users without hassle. However when you are using WP-Cache plugin also in the same time

Sphere: Related Content