0

I tried to enqueue some styles on mobile only using wp_is_mobile() function, but it always give me true, Here is the code (From functions.php file) :

    add_action('wp_enqueue_scripts','load_All_Styles'); // Set up styles

    function load_All_Styles() {
    wp_register_style('font-awesome',get_template_directory_uri().'/css/font-awesome.min.css',array(),false);
    wp_register_style('preloader',get_template_directory_uri().'/css/preloader.css',array(),false);
    wp_register_style('bootstrap',get_template_directory_uri().'/css/bootstrap.min.css',array(),false);

    if(wp_is_mobile()){
        wp_enqueue_style( 'font-awesome' );

    }
    if(is_front_page()) {
        wp_enqueue_style( 'preloader' );
    }
}

what method to use instead? Any Solutions?

2
  • try to debug your code first and what you get?
    – Jevuska
    Commented Dec 18, 2016 at 11:56
  • Have you debug result of wp_is_mobile, that you see why is it true?
    – bueltge
    Commented Dec 19, 2016 at 8:34

1 Answer 1

1

If you look at the code of wp_is_mobile you will see that this returns true based on the user agent sent by the browser. It looks for the word 'mobile' in the user agent's name or some other strings that point to mobile devices, like 'android' and 'kindle'. So, that should work.

However, if you are using any caching plugin that plugin will serve the page regardless of the user agent. After all, caching wouldn't make sense if you would rebuild the page every time a different user agent came in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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