Skip to Content

is_os()

Compare the user’s operating system.

PHP May 3, 2017

Usage

PHP
nebula()->is_os($os, $version, $comparison)

Parameters

$os
(Required) (String) The operating system
Default: None

$version
(Optional) (Integer) The OS version number
Default: None

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

Parameter Notes

Not passing a version number will compare the name only.

See compare_operator() for valid parameters.

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 117.

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_os"
Need a new filter hook? Request one here.

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

PHP
        public function is_os($os=null, $version=null, $comparison='=='){
            $override = apply_filters('pre_nebula_is_os', null, $os, $version, $comparison);
            if ( isset($override) ){return $override;}

            if ( $this->get_option('device_detection') ){
                if ( empty($os) ){
                    trigger_error('nebula_is_os requires a parameter of requested operating system.');
                    return false;
                }

                switch ( strtolower($os) ){
                    case 'macintosh':
                        $os = 'mac';
                        break;
                    case 'win':
                        $os = 'windows';
                        break;
                    default:
                        $os = 'unknown';
                        break;
                }

                $actual_os = $this->device->getOs();
                if ( !empty($actual_os) ){
                    $actual_version = explode('.', $actual_os['version']);
                    $version_parts = explode('.', $version);
                    if ( strpos(strtolower($actual_os['name']), strtolower($os)) !== false ){
                        if ( !empty($version) ){
                            if ( $this->compare_operator($actual_version[0], $version_parts[0], $comparison) ){ //If major version matches
                                if ( $version_parts[1] && $version_parts[1] !== 0 ){ //If minor version exists and is not 0
                                    if ( $this->compare_operator($actual_version[1], $version_parts[1], $comparison) ){ //If minor version matches
                                        return true;
                                    }
                                    return false;
                                }
                                return true;
                            }
                        }
                        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_os', 'my_custom_is_os', 10, 4); //The last integer must be 1 more than the actual parameters
function my_custom_is_os($null, $os, $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_os', '__return_false');