plugins - Serve different files depending on OSBrowser

I have an e-commerce site that sells software. The software is currently only supported by windows, but we're worki

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?

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?

Share Improve this question edited May 24, 2020 at 21:07 user2682863 asked May 22, 2020 at 21:54 user2682863user2682863 1114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/to-your-app/windows-version.zip';
elseif( $user_agent == 'Mac' ) :
    $app_download = 'http://downloadurl/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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742404630a4437596.html

相关推荐

  • plugins - Serve different files depending on OSBrowser

    I have an e-commerce site that sells software. The software is currently only supported by windows, but we're worki

    3天前
    80

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信