How to Detect Internet Explorer Browser In WordPress

Used before category names. Blog JavaScript WordPress WordPress Development

In the backend (PHP) we can’t detect user browser, but we can do it with JavaScript. To do the job, add the following code in your functions.php file in theme.

function hs_add_ie_class()
{
    ?>
    <script>
        if (-1 !== navigator.userAgent.indexOf('MSIE') || -1 !== navigator.appVersion.indexOf('Trident/')) {
            document.body.classList.add('is-IE');
        }
    </script>
<?php
}

add_action('wp_footer', 'hs_add_ie_class');
Tags: