Dequeue Scripts and Style for Mobile not working?

You should run the conditional tags (e.g. is_front_page()) from inside the callback/function (e.g. remove_default_stylesheet()) — see Where to Use Conditional Tags and the warning here. And there’s no hook named wp_enqueue_style; just use the wp_enqueue_scripts to enqueue/register/dequeue/deregister a stylesheet file.

So I’m not sure the exact conditionals you need, but the following would dequeue/deregister the contact-form-7 style and script files on the front page and for mobile devices only:

add_action( 'wp_enqueue_scripts', 'remove_default_stylesheet', PHP_INT_MAX );
function remove_default_stylesheet() {
    // Remove contact-form-7 on the front page and only for mobile devices.
    if ( is_front_page() && wp_is_mobile() ) {
        wp_dequeue_style( 'contact-form-7' );
        wp_deregister_style( 'contact-form-7' );
    }
}

add_action( 'wp_enqueue_scripts', 'my_deregister_javascript', PHP_INT_MAX );
function my_deregister_javascript() {
    // Remove contact-form-7 on the front page and only for mobile devices.
    if ( is_front_page() && wp_is_mobile() ) {
        wp_dequeue_script( 'contact-form-7' );
        wp_deregister_script( 'contact-form-7' );
    }
}

You can also combine the functions:

add_action( 'wp_enqueue_scripts', 'remove_plugin_scripts', PHP_INT_MAX );
function remove_plugin_scripts() {
    // Remove contact-form-7 on the front page and only for mobile devices.
    if ( is_front_page() && wp_is_mobile() ) {
        // Remove style file.
        wp_dequeue_style( 'contact-form-7' );
        wp_deregister_style( 'contact-form-7' );

        // Remove script file.
        wp_dequeue_script( 'contact-form-7' );
        wp_deregister_script( 'contact-form-7' );
    }
}

Leave a Comment