admin管理员组文章数量:1336416
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 badges1 Answer
Reset to default 1The 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;
本文标签: pluginsServe different files depending on OSBrowser
版权声明:本文标题:plugins - Serve different files depending on OSBrowser 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742404630a2468568.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论