WordPress.org

Ready to get started?Download WordPress

Forums

simple: Browser detection? (11 posts)

  1. davitz
    Member
    Posted 9 years ago #

    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

    <?php
    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)
    david

  2. 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 } ?>

  3. mogwai
    Member
    Posted 9 years ago #

    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)?

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

  5. Firas
    Member
    Posted 9 years ago #

    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.

  6. mogwai
    Member
    Posted 9 years ago #

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

  7. Firas
    Member
    Posted 9 years ago #

    Ok, I think I have this.

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

    <?php

    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);
    require('./wp-blog-header.php');
    ?>

    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. http://example.com/blog).

    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.

  8. mogwai
    Member
    Posted 9 years ago #

    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...

  9. Leffe
    Member
    Posted 9 years ago #

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

  10. Gamehall
    Member
    Posted 7 years ago #

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

  11. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    (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" />
    <![endif]-->

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

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

    "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>
    <![endif]-->

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic