A plugin vendor I'm getting support from recently suggested that if I didn't want a certain Javascript file within the plugin to be enqueued, I should go into the plugin's files and comment out the enqueueing. However, this will potentially have to be repeated each time I update the plugin. Is there a way to un-enqueue a plugin's script from functions.php in my child theme so that the change will "stick"?
1
-
If you know the handle of the script you could try using the wp_dequeue_script() function -which is what "intertainment for you" below seems to be suggesting- to remove it. However, if the plugin uses OOP code then it might be a little more difficult. You'd need to provide more details so we can help.cabrerahector– cabrerahector2022-04-26 13:33:44 +00:00Commented Apr 26, 2022 at 13:33
Add a comment
|
3 Answers
Yes there is, the native WordPress function is called wp_dequeue_script.
Remove a previously enqueued script.
@see https://developer.wordpress.org/reference/functions/wp_dequeue_script/
Example
add_action( 'init', function () {
wp_dequeue_script( 'jquery-ui-core' );
} );
1 Comment
user3169905
This should work, but it looks like the plugin developer did not provide a proper handle to reference. I've decided to report the issue to the developer and wait for them to update their plugin to include a handle.
function wp_dequeue_script( $handle ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
wp_scripts()->dequeue( $handle );
}
3 Comments
amarinediary
You can't re-register a native function.
Vipin Singh
Remove a previously enqueued script. @see developer.wordpress.org/reference/functions/wp_dequeue_script add_action( 'init', function () { wp_dequeue_script( 'jquery-ui-core' ); } );
YourManDan
The code copied in this documentation is what the
wp_dequeue_script() function does, not what you should write in order to make it work. Adding this code to your functions.php file will result in errors because this function already exists.