0

I wonder what the best practice is, if it does matter. Should I detect the device first and surround the page block inside of the device block, or the opposite?

The device detector is based on: https://github.com/serbanghita/Mobile-Detect, Mobble plugin.

Device block:

<?php if ( is_mobile() ) { ?>
  mobile
<?php } elseif ( is_tablet() ) { ?>
  tablet
<?php } else { ?>
  computer
<?php } ?>

Page block:

<?php if ( is_home() ) : ?>
  blogpage
<?php elseif( is_page() ) : ?>
  pages
<?php elseif( is_single() ) : ?>
  posts
<?php elseif ( is_category() or is_tag() ) : ?>
  category- or tag-pages
<?php endif; ?>

For examle:

<?php if ( is_mobile() ) { ?>

  <?php if ( is_home() ) : ?>
    blogpage
  <?php elseif( is_page() ) : ?>
    pages
  <?php elseif( is_single() ) : ?>
    posts
  <?php elseif ( is_category() or is_tag() ) : ?>
    category- or tag-pages
  <?php endif; ?>   

<?php } elseif ( is_tablet() ) { ?>

  tablet

<?php } else { ?>

  computer

<?php } ?>
  • 1
    Why you detect the device? That's is not really solid. Everyday get it new devices. If it is a topic of the design you should create a responsive theme, different views maybe but not in depending on the device. – bueltge Nov 27 '16 at 20:03
  • The reason is that I want to cut off the code for the mobile devices and optimize it. And also have an different type of content with data. – M. Sundstrom Nov 27 '16 at 20:26
  • Use less code possible? – Samuel Elh Nov 27 '16 at 22:33
  • Yes, use less code. I'm from Sweden... – M. Sundstrom Nov 27 '16 at 23:25
  • You want to avoid user agent sniffing completely - there is no "better" way otherwise. – TheDeadMedic Nov 28 '16 at 9:47
0

Disregarding for a moment the comments telling you to avoid user agents (because they are error prone and will probably fail on some devices - as you can see from the constant stream of bug fixes on the plugin), the answer to your narrower question is: it doesn't matter.

If A and B are independent of eachother, as in this case, there is a logical equivalence between if A then if B do C and if B then if A do C and if (A and B) do C.

It could be different if a lot of processing time was involved in checking the user agent and there would be a page condition where you didn't need a different version for mobile. But as you state it, you always have to check both user agent and page type before you start doing something. Then the order doesn't matter.

  • Then I have a follow question. WordPress offer a function that use USER_AGENT wp_is_mobile() on function/wp_is_mobile. Is there a specific problem within USER_AGENT or is it specific the USER_AGENT? Where can I read about supported devices and the topic development? – M. Sundstrom Nov 29 '16 at 21:13
  • The problem is that there are new devices and hence new user_agents almost daily. Therefore, having your site depend on detecting a user_agent is not reliable. The plugin you mention, for instance, doesn't recognize increasingly popular Xiaomi smartphones, so pc content will be delivered to those. – cjbj Nov 30 '16 at 12:28
  • I see, so the best thing is to reliable on css, for example: @media only screen and (max-width: 993px){.container{width:90%}}. Thanks for your answers! – M. Sundstrom Nov 30 '16 at 13:51

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.