WordPress.org

Support

Support » Plugins and Hacks » Hacks » PHP conditional statement for User Agent / browser?

PHP conditional statement for User Agent / browser?

  • I want to present some text only if a visitor’s user agent is Internet Explorer.

    I found this plugin, but I was wondering if I can do it without a plugin by using a PHP conditional statement.

    thanks,
    Mike

Viewing 12 replies - 1 through 12 (of 12 total)
  • You should be able to use the same PHP conditionals as that plugin does.

    OK, I tried this:
    <?php if ( is_IE ) <p>words here</p>; ?>
    …but it didn’t work, generated an error

    Mike,

    This isn’t working because is_IE() is a function defined by the plugin you linked to. In order to get it to work correctly, you’ll need to look closely at the code that plugin uses for the function is_IE().

    You can do it with this:

    <?php
    function detect_IE()
    {
    if(isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') !== false))
    {
    //blablabla
    }
    }
    ?>

    I tried to just use the plugin, but I can’t activate it because my theme already has an “is_IE” function defined. I tried changing the plugin function to “is_MSIE”, and using that in my conditional statement, but that didn’t work either.

    Jeez, I would have thought this would be pretty simple, but nothing can be simple, eh? (At least not for me!) 😉

    Oh Pranav, just saw your post…tried that too and got an error. I’m just trying to put this in a php enabled widget to show certain text if the browser is IE. What I’m I missing? (Can you tell I’m a PHP-newbie?)

    thanks,
    Mike

    WordPress does browser detection already, just check against the declared var..

    <?php if( $is_IE ) : ?>
    
    <!-- your html replaces this line -->
    
    <?php endif; ?>

    Thanks Mark, I thought WP did browser detection w/o need for a plugin. However, I tried your code but couldn’t get it to work. I read somewhere that it should be $is_winIE, but that didn’t work either. I must be missing something.

    Mike, what error are you getting from my code? Can you paste it here?

    You may need to globalise the variable first…

    global $is_IE;

    You can see the variable declared on lines 63[1] and 69[2].

    Current
    [1] – https://core.trac.wordpress.org/browser/tags/3.0.1/wp-includes/vars.php
    Trunk
    [2] – https://core.trac.wordpress.org/browser/trunk/wp-includes/vars.php

    Thanks for all the help everybody. Turns out I won’t need this after all.

    Here’s the backstory, if you’re wondering WTF… After a theme “upgrade”, my site started rendering strangely on IE. So I was hoping to quickly implement a message to IE users to “pardon our mess” while I fixed the issue. Turns out that implementing this conditional statement was more difficult (at least for me) than fixing the theme to work in IE. I was able to accomplish the latter before the former, so I no longer need the former.

    So…thanks anyway!

    Well glad to hear you resolved your problem in any case, the issue with the var was probably just a case of code placement as i’ve never had any problem using them for browser detection..

    😉

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘PHP conditional statement for User Agent / browser?’ is closed to new replies.