开发者

Wordpress custom functions help

开发者 https://www.devze.com 2023-02-03 23:52 出处:网络
How do I transform this code to work in Wordpress? I need to use the if statements in the theme template files:

How do I transform this code to work in Wordpress? I need to use the if statements in the theme template files:

function iOSDetect() {

 global $device;
 $browser = $_SERVER['HTTP_USER_AGENT'];
 if(strstr($browser, 'iPhone') || strstr($browser, 'iPod')) {
     $device 开发者_运维百科= 'iPhone';
  } else { $device = 'default'; } 

}

iOSDetect();

if($device == 'default') {
 // Do something
} else { /* Do something else */  }


try it this way:

$device = '';

iOSDetect();

if($device == 'default') {
 // Do something
} else { /* Do something else */  }

Or, you can use a better way:

function iOSDetect() {

 global $device;
 $browser = $_SERVER['HTTP_USER_AGENT'];
 if(strstr($browser, 'iPhone') || strstr($browser, 'iPod')) {
     $device = 'iPhone';
  } else { $device = 'default'; } 
  return $device;
}

and then, use it this way:

if(iOSDetect() == 'default') {
 // Do something
} else { /* Do something else */  }


It looks like you'll just call the function once, so it's not like you really need it to be a function.

Also, that looks like kind of a global vars abuse to me.

$browser = $_SERVER['HTTP_USER_AGENT'];
if(strstr($browser, 'iPhone') || strstr($browser, 'iPod')) {
   $device = 'iPhone';
} else { 
   $device = 'default'; 
} 
if($device == 'default') {
   // Do something
} else { 
   // Do something else 
}

BTW, if the thing to do is just one (loading different css/js files) you can simplify:

$browser = $_SERVER['HTTP_USER_AGENT'];
if(strstr($browser, 'iPhone') || strstr($browser, 'iPod')) {
   //load iphone files
} else { 
   //load standard files 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消