Skip Navigation

[Resolved] Disable unused scripts

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 5 years ago.

Assisted by: Nigel.

Author
Posts
#1394555

Hi, is it possible to disable this script from loading on frontend/backend? Is it safe? And how can I do that?

hidden link
hidden link

I am using only form with text fields and Textarea, nothing else.

Load only on single CPT where form is used:
hidden link
hidden link

And maybe this one too
hidden link

Thanks for advice

#1394701

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

There aren't any options where you can disable particular scripts or stylesheets from loading.

But the assets are added using normal WordPress enqueuing, which means they can be dequeued using wp_dequeue_script or wp_dequeue_style.

See
https://developer.wordpress.org/reference/functions/wp_dequeue_script/
https://developer.wordpress.org/reference/functions/wp_dequeue_style/

Is that something you are familiar with?

#1395159

Hi, I will try it. So it should be easy to do like this:

function cleanup_scripts() {
 
    if ( !is_admin() ) {
 
        wp_dequeue_script( 'date' );
        wp_dequeue_script( 'main' );
        wp_dequeue_script( 'datepicker.min' );
 
    }
}
add_action( 'wp_print_scripts', 'cleanup_scripts', 100 );

is it right?

#1395965

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

It's a little more complicated than that.

Scripts are registered with a "handle" that is some text string that may be quite difference to the js filename, and you need the handle to dequeue the script.

So, the date.js script, for example, is registered with the handle 'wptoolset-field-date', and you would dequeue it with:

wp_dequeue_script( 'wptoolset-field-date' );

So, you need to identify the handle before you can dequeue it, and you may find with a script like main.js that can be many such scripts added and you need to know which one (by handle) that it is you want to dequeue.

Also, an additional complication is dependencies.

So the handle for datepicker.min.js is 'jquery-ui-datepicker', but the following doesn't work, it still gets added to the page:

wp_dequeue_script( 'jquery-ui-datepicker' );

That's because other scripts may say they depend on it, in which case it will still get added to the page, and the only way to remove it is to identify the scripts which depend upon it, and see if it is okay to dequeue them first.

To help with this, you can dump the whole list of registered scripts and their dependencies to your debug.log and inspect it to identify the handles and the dependencies, using the following:

add_action( 'wp_print_scripts', 'ts_dequeue_scripts' );
function ts_dequeue_scripts(){

    global $wp_scripts;
    error_log(print_r($wp_scripts->registered, true));
}