Conditionally load CSS/JS/PHP in wp-admin if using a mobile device

I'm looking for a way to conditionally load different scripts if the user is accessing the admin area from a mobile device.

In my case I'm looking to restrict the user to just be able to make new posts, so if you log in to the admin area from a mobile device you're redirected to the post listing and the admin menu isn't even available.

Ideas?

Topic admin wp-admin mobile conditional-content Wordpress

Category Web


While @s_ha_dum's answer is not only not wrong, but exactly the answer you'd expect to get, I'd highly advise against using it, whether you are on 3.4+ or not.

User agent sniffing sucks. Whether done manually or via a core function. Period.

For one, new UA strings pop up like wildfire. For another, a good number of browsers pretend to be something else anyway.

For another:

  • What is a mobile device?
  • Old phones only?
  • All smart phones?
  • Does that include tablets?

You see, it is impossible to identify a mobile device and categorize it into either being one or not. The only reliable way of serving conditional content is via screen- or viewport-width, respectively. Pixel-density is another factor to consider, but ought not to apply to your issue.

To stray from negativity and provide something somewhat useful: Hence, I'd suggest hiding admin menu items via CSS mediaqueries and/or JS.


wp_is_mobile should be the switch you need, but you may be talking about a lot of edits to a lot of components to do what you describe. I don't know enough about the rest of your code to guess at what kinds of things you would need.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.