1

I have an e-commerce site that sells software. The software is currently only supported by windows, but we're working on bringing it to MacOS. Once it's available we'd like to differentiate the download so that windows users get the windows installer and mac users get the mac app. Is there a plugin that would enable this?

1

The following will help you identify both the OS and the Browser and add classes to the body tag that you can reference when loading the page. You could modify the whole process so that it determines which download link to use. This is what I've used for CSS differentiation purposes, but ultimately what you want is the identification of the OS.

strtolower( $_SERVER['HTTP_USER_AGENT'] ); identifies the browser being used. getenv( 'HTTP_USER_AGENT' ); gets the environment/OS.

<?php 
function os_browser_body_class( $class ) {
        $brwsr = array(
            'msie',
            'firefox',
            'webkit',
            'opera'
        );
        $os = array(
            'Windows',
            'Mac', //will include Mac OS & Apple iOS
            'Macintosh' //just Mac OS
        );
        $user_brwsr = strtolower( $_SERVER['HTTP_USER_AGENT'] );
        $user_agent = getenv( 'HTTP_USER_AGENT' ); 
        foreach( $brwsr as $name ) {
            if( strpos( $user_brwsr, $name ) > -1 ) {
                $class[] = $name;
                preg_match( '/' . $name . '[\/|\s](\d)/i', $user_brwsr, $matches );
                if ( $matches[1] )
                $class[] = $name . '-' . $matches[1];
                return $class;
            }
        }
        foreach( $os as $name ) {
            if( strpos( $user_agent, $name ) > -1 ) {
                $class[] = $name;
                preg_match( '/' . $name . '[\/|\s](\d)/i', $user_agent, $matches );
                if ( $matches[1] )
                $class[] = $name . '-' . $matches[1];
                return $class;
            }
        }
        return $class;
    }
    add_filter( 'body_class', 'os_browser_body_class' );
?>

You could simplify it as well with something like:

<?php 
$user_agent = getenv( 'HTTP_USER_AGENT' ); 
if( $user_agent == 'Windows' ) :
    $app_download = 'http://downloadurl.com/to-your-app/windows-version.zip';
elseif( $user_agent == 'Mac' ) :
    $app_download = 'http://downloadurl.com/to-your-app/mac-version.zip';
endif;  
?>

Then on your page you'd just use an echo to put the proper link in place:

echo $app_download;

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.