I'm trying to prevent loading of extra site menus on mobile. I'd rather not load them at all as opposed to loading them and hiding them with css. I'm starting with this in my header file:

<?php if(wp_is_mobile()) : ?> <?php /*?>Don't load full menus on mobile<?php */?> <?php else : ?> //Code for additional menus goes here <?php endif; ?>

It works, but I pushed this change on Friday - and our site is timing out this week. It could be totally unrelated, but I found a statement in the codex that says " It also should not be used for themes." - https://codex.wordpress.org/Function_Reference/wp_is_mobile

Does this mean I can't use the function in this way? I found several references / examples like the logic I used. If not in a theme, how can I use it?

Bottom line... what is the best way to prevent loading of content on mobile?

  • Have you seen this very similar question?
    – fuxia
    Aug 5 '15 at 22:11
  • I had not seen that one - though I did see a few others. Your link definitely has a more in-depth explanation. So my bottom line is I can't use php to decide what content to load for mobile? I was really hoping to avoid loading and hiding.
    – heytricia
    Aug 5 '15 at 23:26
  • The JavaScript method loads the content only for larger viewports.
    – fuxia
    Aug 5 '15 at 23:29
  • The menus are loaded via php functions. I'm not sure how to bridge the gap from php load to js. I'm thinking this is a brick wall. Unless you have a magic trick you could share? ;-)
    – heytricia
    Aug 6 '15 at 16:35

Yes, you shouldn't use it in your theme. It's used in WP core on administration side for adding touch scripts, mobile button etc. It's very dumb, it just detects User Agent string which can anybody change. And the biggest why not to use it is that you can have a trouble if you use it with a cache plugin because it can show mobile version when desktop version should showed.. It's safe to use it in backend because all caching plugins caching only frontend not backend.

I think that the safest method would be input all content and show/hide whatever you want via media queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.