#59370 closed enhancement (fixed)
Update wp_is_mobile() to account for Sec-CH-UA-Mobile request header
Reported by: | westonruter | Owned by: | westonruter |
---|---|---|---|
Milestone: | 6.4 | Priority: | normal |
Severity: | normal | Version: | 3.4 |
Component: | General | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description
The wp_is_mobile()
function currently uses User-Agent sniffing to determine whether the client is a mobile device. There is a new more reliable way to determine this: Client Hints. Specifically, there is a Sec-CH-UA-Mobile HTTP request header which gets the value of ?1
for a mobile device and the value of ?0
for a desktop device. This header is supported by ~70% of browsers today. It is supported in Chrome, Edge, and Opera. It is not currently supported by Safari or Firefox. Nevertheless, if the function can just be extended to support this request header as its first case, and then fall back to the existing User-Agent sniffing:
-
src/wp-includes/vars.php
diff --git a/src/wp-includes/vars.php b/src/wp-includes/vars.php index 117f053278..9b00d8c750 100644
a b $is_iis7 = $is_IIS && (int) substr( $_SERVER['SERVER_SOFTWARE'], strpos( $_SERVE 146 146 * @return bool 147 147 */ 148 148 function wp_is_mobile() { 149 if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) { 149 if ( isset( $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ) ) { 150 $is_mobile = ( '?1' === $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ); 151 } elseif ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) { 150 152 $is_mobile = false; 151 153 } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) // Many mobile devices (all iPhone, iPad, etc.) 152 154 || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Android' )
Change History (8)
This ticket was mentioned in PR #5226 on WordPress/wordpress-develop by @westonruter.
17 months ago
#1
- Keywords has-patch has-unit-tests added
#4
@
17 months ago
- Summary changed from Update wp_is_mobile() to account for Sec-Ch-Ua-Mobile request header to Update wp_is_mobile() to account for Sec-CH-UA-Mobile request header
This ticket was mentioned in Slack in #core-performance by westonruter. View the logs.
17 months ago
@westonruter commented on PR #5226:
17 months ago
#7
Committed in r56638
Trac ticket: https://core.trac.wordpress.org/ticket/59370