Skip to Content

is_browser()

Compare the user’s browser version.

PHP May 3, 2017

Usage

PHP
nebula()->is_browser($browser, $version, $comparison)

Parameters

$browser
(Required) (String) The browser name
Default: None

$version
(Optional) (String) The browser version number
Default: None

$comparison
(Optional) (String) The comparison operator
Default: "=="

Parameter Notes

Not passing a version number will compare the name only.

Request or provide clarification »

Additional Notes

Note: This function requires the Device Detection Nebula option to be enabled or only basic detection happens with WordPress core.

Source File

Located in /libs/Utilities/Device.php on line 279.

1 Hook

Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

Filters
"pre_nebula_is_browser"
Need a new filter hook? Request one here.

Actions
This function has no action hooks available. Request one?

PHP
        public function is_browser($browser=null, $version=null, $comparison='=='){
            $override = apply_filters('pre_nebula_is_browser', null, $browser, $version, $comparison);
            if ( isset($override) ){return $override;}

            switch ( strtolower($browser) ){
                case 'ie':
                    $browser = 'internet explorer';
                    break;
                case 'ie7':
                    $browser = 'internet explorer';
                    $version = '7';
                    break;
                case 'ie8':
                    $browser = 'internet explorer';
                    $version = '8';
                    break;
                case 'ie9':
                    $browser = 'internet explorer';
                    $version = '9';
                    break;
                case 'ie10':
                    $browser = 'internet explorer';
                    $version = '10';
                    break;
                case 'ie11':
                    $browser = 'internet explorer';
                    $version = '11';
                    break;
                default:
                    break;
            }

            if ( $this->get_option('device_detection') ){
                if ( empty($browser) ){
                    trigger_error('nebula_is_browser requires a parameter of requested browser.');
                    return false;
                }

                $actual_browser = $this->device->getClient();
                if ( !empty($actual_browser) ){
                    $actual_version = explode('.', $actual_browser['version']);
                    $version_parts = explode('.', $version);
                    if ( strpos(strtolower($actual_browser['name']), strtolower($browser)) !== false ){
                        if ( !empty($version) ){
                            if ( $this->compare_operator($actual_version[0], $version_parts[0], $comparison) ){ //Major version comparison
                                if ( !empty($version_parts[1]) ){ //If minor version exists and is not 0
                                    if ( $this->compare_operator($actual_version[1], $version_parts[1], $comparison) ){ //Minor version comparison
                                        return true;
                                    } else {
                                        return false;
                                    }
                                } else {
                                    return true;
                                }
                            }
                        } else {
                            return true;
                        }
                    }
                }
            }

            //Use basic detection (WordPress core) if Device Detect is not enabled. This only checks browser name (not name and version like above)
            if ( empty($version) && $this->get_browser() == strtolower($browser) ){
                return true;
            }

            return false;
        }

Override

To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

PHP
add_filter('pre_nebula_is_browser', 'my_custom_is_browser', 10, 4); //The last integer must be 1 more than the actual parameters
function my_custom_is_browser($null, $browser, $version, $comparison){ //$null is required, but can be ignored
    //Write your own code here

    return true; //Return true to prevent the original function from running afterwords
}

You can completely disable this PHP function with a single line actions:

PHP
 add_filter('pre_nebula_is_browser', '__return_false');