|

WordPress Helper Functions for Detecting IE

The other day I was working on a problem where I wanted to check if a website was using a specific browser (in this case IE) and version (in this case 9 or below). I came up with 2 functions that would serve an a nice, reusable check for both. These can also be extended to check for other browsers or versions, or even accept custom regular expressions.

Before I get started, it’s worth noting that WordPress actually has built-in checks for these already: WordPress Browser Detection Booleans. I used these in conjuction with a function I wrote to detect “old” versions of IE:

function jlc_is_old_ie() {
  global $is_IE;
  return ( $is_IE && jlc_get_ie_version() <= 9 );
}

This function uses a combination of WordPress' built-in $is_IE and use of another function that I wrote to grab the version from the user agent, with help from this WPBeginner tutorial.

function jlc_get_ie_version() {
   preg_match( '/MSIE ([0-9]+)([a-zA-Z0-9.]+)/', $_SERVER['HTTP_USER_AGENT'], $version_no );
   return $version_no[1];
}

This looks for MSIE before a number or letter and numbers and returns the number/letters and numbers found. It's worth noting that this only works for older versions (pre-11) IE. That uses the term Trident in it as well as "Mozilla," but as this blog post points out, User Agent (UA) isn't always the best method. I use it here for 2 reasons: I wanted to specifically target older versions of IE and I'm not changing any specific features; I'm added classes to the body and displaying a message to older browsers that, "Hey, things will definitely look weird for you."

Similar Posts

  • |

    Quick Tip: Add a Widget to WordPress

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!A couple of weeks ago I was working on a WordPress site and thought it might be nice to add a…

  • A Stellar(WP) Sale – 40% Off Everything

    My favorite all-around WordPress company, StellarWP (parent company: LiquidWeb), is celebrating their official launch with a 40% off sale, now through August 4th with the code STELLARSALE. This is a great opportunity to pick up some tools I strongly recommend.

  • |

    Add Attachments to WordPress Search Results

    I feel like this has to have been done a lot, and there are great plugins out there for it, but if you’re just looking to add a quick function to your theme (or a really simple plugin) yourself, here’s how to modify WordPress’ search query to include attachments (like images). function attachment_search( $query ) {…

  • |

    Announcing WP in One Month WordPress Courses

    I started WP in One Month almost a year ago, and since then it’s gone through 2 major evolutions; I wanted to find the right model. The first was moving from live classes to live webinars. Today, I’m excited to announce the next one: the addition of online WordPress courses.

  • |

    New Plug-in: Latest Post Redirect

    One thing I often forget to do is update social links when I’m promoting a new blog post or podcast episode. Especially on Instagram, I’ll say something like, “link in the profile,”1 only to forget to add the link to the profile! Services like Linktree help a little, but you still need to update. Well…