WordPress Switch Themes for different Browser User Agent

You want to show specific themes to users based on their Browser? No problem. We have a solution for you. To do this first we have to find out which browser the user is using, then we will redirect the user to a theme specially designed for her/him. How to find the user’s browser details? What is the name of the browser? and which version is it? Don’t worry it is also easy if you know it.

Get the user’s browser details

Of course this is the first thing we have to do. We have to get the details about the browser that the user is using to visit our site. The following one line of PHP script will get the client’s browser details.

$_SERVER['HTTP_USER_AGENT']
So how we can use it to get what we want?

Switching to a different theme Based on the Browser User Agent

WordPress is very flexible and if you can modify it then you can achieve amazing results from it. WordPress supports plugins(yes we need it do it the WordPress way) and we will use a custom plugin to deliver a theme based on the user’s browser.
Copy this code and paste it in to a *.php file. Name it anything you want and upload it to wp-content/plugins directory.

<?php
/*
Plugin Name: Theme Switcher based on User Browser
Description: Switch to different themes based on the User's Web Browser.
Author: Rabin Biswas
Author URI: http://updateox.com/
*/
add_filter('template', 'theme_switcher');
add_filter('option_template', 'theme_switcher');
add_filter('option_stylesheet', 'theme_switcher');
function theme_switcher($theme) {
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') == true){
$theme = 'theme_ie';
}
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') == true){
$theme = 'theme_chrome';
}
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla') == true){
$theme = 'theme_firefox';
}
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') == true){
$theme = 'theme_opera';
}
return $theme;
}
?>

Before you activate the plugin make sure you have uploaded all themes necessary to this. You can add more lines for more browsers or delete lines for less browsers.
To change themes based on browser version rather than browser family change the line

(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') == true)

to

(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') == true)

Now whenever a User visits your site using Firefox browser he/she will see a theme specially designed for Firefox. If the user usages Chrome then he/she will see the Chrome theme.

Add a Comment

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.