Make WordPress Core

Opened 17 months ago

Closed 17 months ago

Last modified 16 months ago

#59370 closed enhancement (fixed)

Update wp_is_mobile() to account for Sec-CH-UA-Mobile request header

Reported by: westonruter's profile westonruter Owned by: westonruter's profile 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 
    146146 * @return bool
    147147 */
    148148function 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'] ) ) {
    150152                $is_mobile = false;
    151153        } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) // Many mobile devices (all iPhone, iPad, etc.)
    152154                || 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

#2 @westonruter
17 months ago

  • Milestone changed from Future Release to 6.4

#3 @westonruter
17 months ago

  • Owner set to westonruter
  • Status changed from new to accepted

#4 @westonruter
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

#6 @westonruter
17 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 56638:

General: Account for Sec-CH-UA-Mobile client hint request header in wp_is_mobile().

Add missing test coverage for wp_is_mobile().

Fixes #59370.
Props westonruter, flixos90.

@westonruter commented on PR #5226:


17 months ago
#7

Committed in r56638

This ticket was mentioned in Slack in #core-performance by westonruter. View the logs.


16 months ago

Note: See TracTickets for help on using tickets.