0

I've followed a few tutorials on Dequeueing and Deregistering scripts and styles for WP - trying to optimise my site speed.

Initially I could dequeue and deregister, until I added a condition for mobile only sites. Now the functions won't work.

    add_action( 'wp_enqueue_style', 'remove_default_stylesheet', PHP_INT_MAX);
    if ( is_front_page() && wp_is_mobile() ) {    
        function remove_default_stylesheet() {

            wp_dequeue_script( 'contact-form-7' );
            wp_deregister_script( 'contact-form-7' );        

    }
    } elseif ( is_front_page() ) {
        function remove_default_stylesheet() {

            wp_dequeue_style( 'contact-form-7' );
            wp_deregister_style( 'contact-form-7' );

    }
    }

    add_action( 'wp_enqueue_scripts', 'my_deregister_javascript', PHP_INT_MAX);
    if ( is_front_page() && wp_is_mobile() ) {
        function my_deregister_javascript() {

            wp_dequeue_script( 'contact-form-7' );
            wp_deregister_script( 'contact-form-7' );

        }

    } elseif ( is_front_page() ) {

    add_action( 'wp_enqueue_scripts', 'my_deregister_javascript', PHP_INT_MAX);
function my_deregister_javascript() {

        wp_dequeue_script( 'contact-form-7' );
        wp_deregister_script( 'contact-form-7' );    

    }
} 

I'm trying to dequeue and deregister scripts and styles depending on whether it's the homepage, and if the user is on mobile.

I can't understand why this isn't working despite searching about

0

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' );
    }
}
  • 1
    This worked for me nicely - thanks for providing a clean solution as well as an explination! – PublicDisplayName Jul 30 at 20:37

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.