Du musst dich anmelden um Beiträge und Themen zu erstellen. Einloggen · Registrieren

Browser Detection via WordPress’ body_class Function

WordPress makes it possible to detect a handful of different browsers using a variety of built-in global variables. WordPress also provides a body_class tag that outputs a variety of class attributes depending on various page properties. Why not combine these two techniques so that the user’s detected browser is added to the list of output classes for the body tag? Here’s the code that will do it via functions.php:

// browser detection via body_class
function browser_body_class($classes) {

    global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

    if($is_lynx)       $classes[] = 'lynx';
    elseif($is_gecko)  $classes[] = 'gecko';
    elseif($is_opera)  $classes[] = 'opera';
    elseif($is_NS4)    $classes[] = 'ns4';
    elseif($is_safari) $classes[] = 'safari';
    elseif($is_chrome) $classes[] = 'chrome';
    elseif($is_IE)     $classes[] = 'ie';
    else               $classes[] = 'unknown';

    if($is_iphone) $classes[] = 'iphone';
    return $classes;


This code will output the detected browser along with the other body_class tags. Here is an example:

<body class="home blog logged-in safari">


Sources: Nathan Rice