最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Serve different files depending on OSBrowser

programmeradmin0浏览0评论

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;

发布评论

评论列表(0)

  1. 暂无评论