Quantcast
Channel: Nathan Rice» Planet WordPress
Viewing all articles
Browse latest Browse all 10

Serve IE6 Visitors the Default WordPress Theme

0
0

Internet ExplorerIt’s been talked about time and time again … what do we do about the IE6 problem?

Of course, there are a couple of options already available to you: you can make IE6 crash when users visit, you can move on to bigger and better things and ignore IE6 altogether, or you can do like I do and display a little message to IE6 visitors encouraging them to upgrade (this plugin makes that pretty easy).

But, if you’re not in the business of pandering to inferior browsers, but you don’t necessarily want to be seen as a jerk, I have a solution that might work out perfectly for you.

Let’s Do Some Browser Detection

True, it’s not perfect. But as we’ve pointed out before, browser detection can go a long way towards delivering a smooth experience for users of all browsers.

What we want to do is detect the visitor’s “User Agent”, then tell WordPress to use the default theme (that is, the Kubrick theme) whenever an IE6 user visits our site. So, we do a little PHP magic to detect the User Agent.

if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false) {
	// do something
}

This IF statement will evaluate whether or not the user’s browser is reporting IE6 as the User Agent. It should be noted that some browsers allow users to spoof their User Agent. So it is theoretically possible that someone is spoofing an IE6 User Agent and forgot to switch back. Possible, but very unlikely.

Tell WordPress to Use the Default Theme

So, now that we know whether or not the visitor is using IE6, we need to tell WordPress do something with that information. Fortunately, WordPress offers some filters for us to use. The code looks something like this (including the browser check from above):

add_filter('template', 'serve_default_to_iesix');
add_filter('option_template', 'serve_default_to_iesix');
add_filter('option_stylesheet', 'serve_default_to_iesix');
function serve_default_to_iesix($theme) {
	if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false)
		$theme = 'default';

	return $theme;
}

Making the Plugin

Because this bit of functionality is used to determine which theme gets loaded, this is one of the few things that you can’t do using your theme’s functions.php file … you have to actually use a plugin. But no worries … here’s exactly what the plugin code should look like:

<?php
/*
Plugin Name: Serve Default to IE6
Plugin URI: http://www.nathanrice.net/blog/serve-ie6-visitors-the-default-wordpress-theme
Description: This plugin will serve the default theme to any visitors using IE6.
Author: Nathan Rice
Author URI: http://www.nathanrice.net/
Version: 1.0
*/

add_filter('template', 'serve_default_to_iesix');
add_filter('option_template', 'serve_default_to_iesix');
add_filter('option_stylesheet', 'serve_default_to_iesix');
function serve_default_to_iesix($theme) {
	if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false)
		$theme = 'default';

	return $theme;
}
?>

And if you aren’t the best “copy and paste”-er, here’s a download link. Just upload the plugin and activate, and you’re good to go.

Please note that, because this functionality relies on dynamic template serving, it will be incompatible with plugins like WP-Super-Cache.

There you go! Now, whenever someone visits with IE6, they’ll be seeing your site courtesy of the Default theme. But you don’t have to stop there … if there is another theme in your wp-content/themes directory that you’d like to use instead of the default theme, just find its directory/folder name and replace ‘default’ with the directory/folder name of the other theme you’d like to use (case sensitive). For instance, if you’d like to use the “classic” theme, you’d modify the code above like so:

...
	if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false)
		$theme = 'classic';
...

Pretty cool, eh?


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images