Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#52397 new enhancement

The function wp_is_mobile should accept also lower case strings as user agents

Reported by: giuse's profile giuse Owned by:
Milestone: Awaiting Review Priority: low
Severity: normal Version: 3.4
Component: General Keywords: reporter-feedback
Focuses: Cc:

Description

The actual version of the function wp_is_mobile is case sensitive and detects a mobile device only if the first letter of the User Agent is capital.

https://developer.wordpress.org/reference/functions/wp_is_mobile/

For example, I would replace

strpos( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) !== false

with

strpos( strtolower( $_SERVER['HTTP_USER_AGENT'] ),'mobile' ) !== false

The same for the other User Agents.

Change History (4)

#1 @sabernhardt
3 years ago

  • Version changed from 5.6 to 3.4

(I updated the version because the function has been around since WordPress 3.4 - changeset:20417)

Could the stripos function be appropriate and/or better here?

#2 @johnbillion
3 years ago

  • Keywords reporter-feedback added
  • Priority changed from normal to low

Which user agents does this affect?

#3 @giuse
3 years ago

I opened this thread because W3 Total Cache was giving problem to my plugin that uses this function.

If you save the option related to the User Agent of W3 Total Cache, it sanitizes it and it is saved "mobile" instead of "Mobile". I know this is something that W3 Total Cache should solve, but my question is: What's the problem in adding a strtolower? The performance would be the same, and you would prevent issues with other plugins. Moreover, you never know if a certain browser uses "mobile" instead of "Mobile". This function would be more robust and prevent issues if it wasn't case sensitive.

I think it would be faster and easier adding strtolower than going ahead commenting on this function, we are not speaking about something that is a risk for performance or security, but if you don't add strtolower I will replace this function with another one in my plugin, so if you don't want to add strtolower, I suggest closing this thread.

Consider that also other plugins may replace this function, this would be not good for the WP ecosystem if the reason is only a missing strtolower.

Version 0, edited 3 years ago by giuse (next)

#4 @giuse
3 years ago

Or better. as suggested by @sabernhardt, you could replace strpos with stripos that is supported from PHP 5+, so no problem with stripos and really no difference in terms of performance.

Note: See TracTickets for help on using tickets.