WordPress.org

Ready to get started?Download WordPress

Forums

PHP conditional statement for User Agent / browser? (13 posts)

  1. Mike
    Member
    Posted 3 years ago #

    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

  2. Tim Moore
    Moderator
    Posted 3 years ago #

    You should be able to use the same PHP conditionals as that plugin does.

  3. Mike
    Member
    Posted 3 years ago #

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

  4. Tim Moore
    Moderator
    Posted 3 years ago #

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

  5. Pranav Rastogi
    Member
    Posted 3 years ago #

    You can do it with this:

    <?php
    function detect_IE()
    {
    if(isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') !== false))
    {
    //blablabla
    }
    }
    ?>
  6. Mike
    Member
    Posted 3 years ago #

    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!) ;-)

  7. Mike
    Member
    Posted 3 years ago #

    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

  8. Mark / t31os
    Moderator
    Posted 3 years ago #

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

    <?php if( $is_IE ) : ?>
    
    <!-- your html replaces this line -->
    
    <?php endif; ?>
  9. Mike
    Member
    Posted 3 years ago #

    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.

  10. Pranav Rastogi
    Member
    Posted 3 years ago #

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

  11. Mark / t31os
    Moderator
    Posted 3 years ago #

    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

  12. Mike
    Member
    Posted 3 years ago #

    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!

  13. Mark / t31os
    Moderator
    Posted 3 years ago #

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

    ;)

Topic Closed

This topic has been closed to new replies.

About this Topic