Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#53395 new defect (bug)

Cannot use wp_is_mobile() in Network activated plugins

Reported by: promz's profile promz Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Plugins Keywords:
Focuses: multisite Cc:


Steps to reproduce

  • Create a network site
  • Create and activate a simple custom plugin, use wp_is_mobile() function anywhere in that plugin
  • Login to the network dashboard and then activate the plugin

You will see an error:

Uncaught Error: Call to undefined function wp_is_mobile()

Looking at the source code of wp_is_mobile(), I can't see anything that would prevent it from loading earlier. Maybe it should be loaded earlier?

Change History (1)

#1 @SergeyBiryukov
3 years ago

  • Component changed from General to Plugins
  • Focuses multisite added

Hi there, welcome back to WordPress Trac! Thanks for the report.

wp_is_mobile() is located in wp-includes/vars.php, which is indeed loaded later than most other PHP files in wp-settings.php.

That said, it is considered a best practice in WordPress not to run any code directly on plugin inclusion, as the environment is not fully initialized yet, and run it on the appropriate action instead, e.g. plugins_loaded or init.

Moving the wp_is_mobile() call in your plugin to a function or method that runs on a specific action should resolve the issue here.

Last edited 3 years ago by SergeyBiryukov (previous) (diff)
Note: See TracTickets for help on using tickets.