Skip to content
Advertisement

WordPress dequeue scripts only on single page

My website is using map functionalities that slow the whole site down dramatically.

I can dequeue them from the entire site with this add_action function:

add_action( 'wp_enqueue_scripts', function() {
    wp_dequeue_script( 'mylisting-google-maps' );
    wp_dequeue_script( 'google-maps' );
    wp_dequeue_style( 'mylisting-google-maps' );
    wp_dequeue_script( 'mylisting-mapbox' );
    wp_dequeue_script( 'mapbox-gl' );
    wp_dequeue_style( 'mapbox-gl' ); 
}, 99 );

My goal now is to dequeue these scripts on all but one page “/add-listing” where I need them so that users can still input their location.

Any tips on how to do that?

Advertisement

Answer

So, you can use something like that:

add_action( 'wp_enqueue_scripts', function() {
    if (!is_page('add-listing') {
        wp_dequeue_script( 'mylisting-google-maps' );
        wp_dequeue_script( 'google-maps' );
        wp_dequeue_style( 'mylisting-google-maps' );
        wp_dequeue_script( 'mylisting-mapbox' );
        wp_dequeue_script( 'mapbox-gl' );
        wp_dequeue_style( 'mapbox-gl' );
    }
}, 99 );
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement