[wp-trac] [WordPress Trac] #59370: Update wp_is_mobile() to account for Sec-Ch-Ua-Mobile request header

WordPress Trac noreply at wordpress.org
Fri Sep 15 23:01:44 UTC 2023


#59370: Update wp_is_mobile() to account for Sec-Ch-Ua-Mobile request header
-------------------------+----------------------------
 Reporter:  westonruter  |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Future Release
Component:  General      |    Version:  3.4
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+----------------------------
 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: [https://developer.mozilla.org/en-
 US/docs/Web/HTTP/Client_hints Client Hints]. Specifically, there is a
 [https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-
 Mobile 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 [https://caniuse.com/mdn-http_headers_sec-ch-ua-mobile
 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:

 {{{#!diff
 diff --git a/src/wp-includes/vars.php b/src/wp-includes/vars.php
 index 117f053278..9b00d8c750 100644
 --- a/src/wp-includes/vars.php
 +++ b/src/wp-includes/vars.php
 @@ -146,7 +146,9 @@ $is_iis7 = $is_IIS && (int) substr(
 $_SERVER['SERVER_SOFTWARE'], strpos( $_SERVE
   * @return bool
   */
  function wp_is_mobile() {
 -       if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
 +       if ( isset( $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ) ) {
 +               $is_mobile = ( '?1' === $_SERVER['HTTP_SEC_CH_UA_MOBILE']
 );
 +       } elseif ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
                 $is_mobile = false;
         } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mobile' )
 // Many mobile devices (all iPhone, iPad, etc.)
                 || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Android' )
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59370>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list