|

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

  • | |

    Random Code Generator 1.0

    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, when I officially launched the redesign, I mentioned the forthcoming release of a plugin I wrote…

  • |

    NEPAWP November Meetup: Blogger Chat!

    This month we are switching things up a bit at the NEPAWP Meetup and bringing it back to basics- we’re talking all about blogging! It will be a pretty open discussion about all things blogging: starting one, running one, SEO, more comments, using social media, and more. If you have a question about blogging, join us…

  • | |

    Deleting 3 Million Spam Comments from a WordPress Database

    Over at the Crowd Favorite blog, I wrote up a post about an interesting problem I solved recently. I laid out everything you need to know there, but it involves downloading a HUGE database and putting WP-CLI to good use. If you haven’t used it before and you do development with WordPress, it’s super valuable….

  • |

    WordPress and .gitignore

    Updates have been added after the jump! In the coming weeks I’ll likely put a lot of my own personal code (as in, not code people pay me to write) in public repos on Github. A lot of my work is WordPress related so I’ll make a local repo at the root of some WordPress…

  • | |

    Your Personal Website

    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!I’m doing a lecture on Monday, telling students how to properly build a website to promote themselves. After the jump is…

  • |

    Migrating WordPress Multisite from Media Temple to SiteGround

    A few years ago, I wrote about domain mapping using WordPress Multisite on Media Temple. This year, I’ve been consolidating all of my hosted websites to a single SiteGround account and the very Multisite instance I wrote about needed to be moved over. I had been avoiding it but the time had come, especially since…