Home >CMS Tutorial >WordPress >How to determine if wordpress is a mobile device
Nowadays, mobile devices are becoming more and more popular and smarter. It has become more popular to use mobile phones to browse the web. Therefore, as a WordPress theme developer, you must carefully consider how to respond. Mobile phone users.
This is a general PHP function for judging mobile browsers. The principle is relatively simple. It is to judge the user_agent returned by the browser. The conditions include mobile phone system, brand and window size.
Take WordPress as an example. Add the following code to the theme’s functions.php. It currently includes useragent for common mobile browsers, which can basically cover the user group who may use mobile phones to access the Internet.
function is_mobile() { $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_browser = Array( "mqqbrowser", //手机QQ浏览器 "opera mobi", //手机opera "juc","iuc",//uc浏览器 "fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod", "iemobile", "windows ce",//windows phone "240x320","480x640","acer","android","anywhereyougo.com","asus","audio","blackberry","blazer","coolpad" ,"dopod", "etouch", "hitachi","htc","huawei", "jbrowser", "lenovo","lg","lg-","lge-","lge", "mobi","moto","nokia","phone","samsung","sony","symbian","tablet","tianyu","wap","xda","xde","zte" ); $is_mobile = false; foreach ($mobile_browser as $device) { if (stristr($user_agent, $device)) { $is_mobile = true; break; } } return $is_mobile;}
Then add the following judgment at the top of any theme template:
<?php if (is_mobile() ): ?> //如何如何..(这里可以添加一个mobile.css,如<link rel="stylesheet" type="text/css" media="all" href="http://www.jqueryba.com/"<?php echo get_template_directory_uri(); ?>/mobile.css" />) <?php endif ;?>
Another thing to note: Whether it is a separate WordPress theme or an adaptive theme, you need to add it in the head The following meta, otherwise it may cause problems such as too small fonts displayed on the mobile phone.
<meta name="viewport" content="width=device-width"/>
For more wordpress-related technical articles, please visit the wordpress tutorial column to learn!
The above is the detailed content of How to determine if wordpress is a mobile device. For more information, please follow other related articles on the PHP Chinese website!