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 | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Plugins | Keywords: | |
Focuses: | multisite | Cc: |
Description
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)
Note: See
TracTickets for help on using
tickets.
Hi there, welcome back to WordPress Trac! Thanks for the report.
wp_is_mobile()
is located inwp-includes/vars.php
, which is indeed loaded later than most other PHP files inwp-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 on the appropriate action instead, e.g.
plugins_loaded
orinit
.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.