1

Would like to ask the question is it possible to stop the laoding of some scripts when viewed on a mobile/tablet device ?

In the theme functions file you have wp_enqueue_script(...). How would you go about loading if based on device/screen size..

Is this possible?

2
  • You can try wp_is_mobile function to check mobile device. Documentation Commented Oct 22, 2014 at 3:26
  • Hi Nilambra, thanks for your reply. do you have an example how this would work with stop loading scripts based on wp_is_mobile ? Commented Oct 22, 2014 at 3:30

3 Answers 3

2

wp_is_mobile() does not really solve your problem completely. To be honest here, there is no logic in Wordpress to achieve what you want

  • First of all, wp_is_mobile() only checks for mobile devices. Tablets are excluded from this

  • Window sizes are determined on browser side, not server side, so there are no way for php to check window size. That is why there are also no function in Wordpress that can detect a window size

The only probable way to do this is to make use of javascript. I've seen examples where javascript is used to determine window size and then do load something accordingly. I haven't tested anything like this yet, and frankly, that would also be off topic here. I would suggest that you try a few google searches and testing out a few solutions as I suggested

0

Try wp_is_mobile function to check, like:

function theme_name_scripts() {
    if( ! wp_is_mobile() ) {
        wp_enqueue_style( 'style-name', get_stylesheet_uri() );
        wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
    }
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
0

Try this:

function custom_load_scripts() {
    // Load if not mobile
    if ( ! wp_is_mobile() ) {
        // Example script
        wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array    (), '1.0.0', true );
    }
}

add_action( 'wp_enqueue_scripts', 'custom_load_scripts' );

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.