WordPress provides dequeuing and deregistering functions for both scripts and styles. This is very useful for reducing the potential of conflicts with plugins and load time.
- wp_deregister_script()
- wp_deregister_style()
- wp_dequeue_script()
- wp_dequeue_style()
Each of these four functions accepts only one parameter: $handle – the unique name of the script or stylesheet you want to deregister or dequeue.
They allow us to remove assets modularly. The following example shows how jQuery can easily be removed and replaced by a more recent version.
function my_scripts() {
wp_deregister_script( 'jquery' );
wp_register_script(
'jquery',
get_template_directory_uri() . '/jquery-recent.js' );
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );
The wp_enqueue_scripts action hook is called to actually load the function. Despite its name, it is used also for removing both scripts and styles in the front-end.
That being said, it is not recommended to replace jQuery with a new version, because WordPress is built to work as smoothly as possible with the version added to it by default.
Leave A Comment