2

I just read the wp_is_mobile function reference and I have some questions. As you can read, there is a line explanation in bold text: It also should not be used for themes. I excactly don't know what it's mean. I just want to implement this in a theme which is combined with css media query for the layout and wp_is_mobile for removing some element.

For example, I want to display slider and sidebar in large window (i.e. desktop) then I want to remove them in mobile device. I can hide them with css propery display: none; visibility: hidden. But I wonder if they're still rendered. In related to the bold line warning above, when and where I must use that function? Ok, I got an idea. How if I create a simple conditional check like this line?

<?php
if ( wp_is_mobile() )
{
    get_template_part( 'mobile-content' );
} else {
    get_template_part( 'content', get_post_format() );
}
?>

First, it checks if the mobile device detected, then my custom template will be rendered mobile-content.php. Otherwise it will render normal template content-{post-format}.php.

Please share your opinion which is the best way to implement wp_is_mobile and combine it in media query. Hope you can get my point.

Best regards.

4
  • I guess that bold line means, wp_is_mobile() function does consider tablet as a mobile device. So the use of this function in a theme is not recommended. – Domain Jun 25 '15 at 9:47
  • 3
    wp_is_mobile() is extemely unreliable and a function that should have never made it into core. It relies on user agent which can be manipulated. If the function fails, the incorrect info is displayed to the incorrect device. Your best solution is to use responsive design – Pieter Goosen Jun 25 '15 at 9:52
  • Ok. But where I put that function if not in a theme? – learn4life Jun 25 '15 at 10:00
  • Thanks @PieterGoosen, I agree with your opinion. Therefore I work with media queries too. I think the use of both media queries and wp_is_mobile() have pros and cons. If I only use media queries, it makes all elements and contents rendered like normally and I think it wasn't reducing page load. Cmiiw – learn4life Jun 25 '15 at 10:09
-1

wp_is_mobile() this function is for code the device on which you viewing site is mobile. i mean this condition will true if you view site in mobile. now as per your question you want to display slider and sidebar only in desktop then your code will be like below:

1. for sidebar

if (!wp_is_mobile() ) {
    get_sidebar();
}

2. for slider    
     if (!wp_is_mobile() ) {
            // slider related stuffs.
}
3
  • Ok. I got yout point. That means I have to write that function multiple times in a file templates whenever I need? I mean for example in is_front_page, is_page, is_single, or is_category. CMIIW – learn4life Jun 25 '15 at 9:57
  • Note, wp_is_mobile() is very unreliable – Pieter Goosen Jun 25 '15 at 9:58
  • exactly. you got that.it's like wordpress conditional tags. – Anand Jun 25 '15 at 9:59

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.