Support » Plugins » simple: Browser detection?

  • Hia!

    Still being a total newbie, I’m having troubles to properly detect a browser. (I need this to serve MSIE a different stylesheet, and some script-stuff).

    I managed to successfully throw a copy of the //Simple browser detection from the file vars.php into the header.php, but of course I’d prefer to use the “original” (already generated) variable that comes out of the vars.php.

    What string would I insert into the headers.php? (I tried stuff like

    if (($is_winIE) || ($is_macIE) {
    <link rel=”stylesheet” href=”<?php bloginfo(‘stylesheet_directory’); ?>/styleMSIE.css” type=”text/css” media=”screen” />
    <?php }
    else {
    <link rel=”stylesheet” href=”<?php bloginfo(‘stylesheet_url’); ?>” type=”text/css” media=”screen” />
    <?php } ?>

    but this does not work unless I previously included the browser-detection…
    (syntax errors in this post are typoes)

    TIA (a lot of)

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator James Huff


    Volunteer Moderator 🚀

    Theme Wuhan does exactly what you’re looking for by placing these lines in the Header template (header.php):

    <?php if (eregi("MSIE",getenv("HTTP_USER_AGENT")) ||
    eregi("Internet Explorer",getenv("HTTP_USER_AGENT"))) { ?><link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_directory'); ?>/style-ie.css"/>
    <?php } else { ?>

    <link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_directory'); ?>/style.css"/>

    <?php } ?>

    Sorry, but as I got no reply on this earlier on I just had to ask again.
    Does anybody know if this is also possible with complete themes?

    i.e. let this script change a theme by adressing the theme switcher plug-in (by ryan boren)?

    Moderator James Huff


    Volunteer Moderator 🚀

    Read the post that I made, just before you posted. It describes the technique used in Theme Wuhan, which is a complete theme.

    mogwai, switching themes based on browser might be a bit complex–you’ll probably need to do it in the root/index.php so that you don’t see a flash of one theme and then see another–but it’s probably possible to send a query to the theme switcher plugin to do it.

    Yes – that’s what my question was about! Sending a query to the theme switcher plug-in. But nobody seems to know how…

    Ok, I think I have this.

    Replace the index.php in your wordpress root with this:


    if(empty($_COOKIE['ThemeSwitched'])) {
    if (eregi("MSIE",getenv("HTTP_USER_AGENT")) || eregi("Internet Explorer",getenv("HTTP_USER_AGENT"))) {
    setcookie('ThemeSwitched', 'true');
    header('Location: /?wptheme=WordPress+Classic');
    } else {
    setcookie('ThemeSwitched', 'true');
    header('Location: /?wptheme=WordPress+Default');

    define('WP_USE_THEMES', true);

    If your WordPress install is not accessible from your site root, then replace the two header('Location: /?wptheme= with header('Location: http://example/?wptheme=, example being the path you put in your browser to access your blog (eg.

    Replace wptheme=WordPress+Classic and wptheme=WordPress+Default with the actual queries for the theme switcher for the themes you want to load. The first query is if the browser is IE, the second is if it’s not IE (so in the example code above, IE gets the Classic theme.)

    To see what query to send to the theme switcher, do a call to <?php wp_theme_switcher(); ?> somewhere in your template and see the URL it points to to load your preferred theme.

    Thanks a lot for the info! I will try this later on. One thing missing is a message that would inform visitors that cookies need to be enabled…

    Include vars.php and use the variables defined in it.

    thanks for the help, someone knows if this techinique work ?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    (I need this to serve MSIE a different stylesheet, and some script-stuff)

    The correct way to do this is to use conditional comment tags in the page itself.
    <!--[if IE]>
    <link rel="stylesheet" type="text/css" media="screen"
    href="/ie-stylesheet.css" />

    You can even be version specific, if you like:
    <!--[if IE 5]>

    Or even a variety of versions:
    <!--[if gte IE 5]>

    “gte” means greater than or equal to. There’s also lt, lte, and gt.

    This doesn’t work with just CSS, but with anything you like:
    <!--[if IE]>
    <h1>Welcome IE User!</h1>

    Firefox and other browsers will ignore these entirely, as they’re in comment tags.

    To work the other way, the non-standard comment tag comes in handy:
    <comment>You are not using IE.</comment>

    IE will ignore the stuff in the comment tags, other browsers won’t.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘simple: Browser detection?’ is closed to new replies.