admin管理员组文章数量:1327660
I'm going to test the user agent to load a mobile template instead of the desktop theme.
There are many mobile detection scripts out there.
- Does
wp_is_mobile()
function work well? - What is your experience in comparison with other classes like e.g.
mobiledetect
?
I'm going to test the user agent to load a mobile template instead of the desktop theme.
There are many mobile detection scripts out there.
- Does
wp_is_mobile()
function work well? - What is your experience in comparison with other classes like e.g.
mobiledetect
?
4 Answers
Reset to default 16Yes it works well. It's a very simple function but never found a mobile device not recognized by it. It recognize the 90%+ of mobile devices. Main difference from mobiledetect is that doesn't differe from phone and tablets.
See the code
Yes, the wp_is_mobile()
works well, but you can run into problems when using aggressive caching systems, such as fastcgi, proxy cache, etc. that skip the php execution.
If that is the case, I would recomend some client side (javascript) detection method that is loaded for both mobile and desktop browsers.
It is a shitty idea to use that function. Device identification should always be done at the client side, and user agent is probably the worst way to identify whether a device supports a feature X or have a form factor Y.
You start using it when the site is young and then your site grows and you decide to cache your HTML and boom all your device detection code stops working..... Better not to walk down that ally in the first place.
Your question specifically asks if wp_is_mobile() is effective; not how device detection should be evaluated. The function just works. So Yes, it is effective. As an example when it may be preferred to client-side detection, even Mozilla thinks that's a bad idea.
It's worth re-iterating: it's very rarely a good idea to use user agent sniffing. You can almost always find a better, more broadly compatible way to solve your problem! mozilla
Is a mobile dectection plugin any better? There may be good reason not to choose mobile detection via a plugin over wp_is_mobile(). First, Wordpress is already doing a lot of the tedius tasks, so just hand this over to it as well!
Prior to loading a DataTable I am using mobile-detect.js as part of my requirement to detect and style when the device is mobile:
// if phone or tablet then set responsive mode
var md = new MobileDetect(window.navigator.userAgent);
if (md.phone() || md.tablet()) {
scrollObj = false;
$('#sometable').removeAttr('width').addClass('dt-responsive');
responsiveObj = {
details: {
type: "inline",
display: $.fn.dataTable.Responsive.display.childRowImmediate
}
};
}
This works fine, but it comes with a small cost to require a plugin for a one time use per session function call. Additionally, it's creator warns about using his plugin.
"you should not use this library in your HTML page and it’s less reliable when used server-side"
So, instead of loading a one-time-use plugin, a more effective way to handle device detection without sniffing is to use wp_is_mobile().
// This is in a JavaScript block in a PHP page
isMobile = Boolean(<?php echo wp_is_mobile() ?>);
本文标签: theme developmentIs wpismobile() effective
版权声明:本文标题:theme development - Is wp_is_mobile() effective? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742228293a2436739.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论