Using wp_deregister_script to deregister JavaScript files in WordPress

The wp_deregister_script function in WordPress is used to remove a script that has been registered in the WordPress system. This function is part of the script management system in WordPress, which allows developers to manage JavaScript files that are included in a WordPress site.

When a script is registered using the wp_register_script function, it is added to the list of scripts that WordPress can enqueue and print in the HTML of a page. There are scenarios where a developer might not want a particular script to be included on certain pages or under specific conditions. In such cases, the wp_deregister_script function can be used to remove the script from the list of registered scripts.

By deregistering a script, it will not be enqueued or printed in the HTML, even if it was previously registered. This can help in avoiding conflicts between different scripts, reducing page load times, or ensuring that only the necessary scripts are loaded on a page.

Parameters

The wp_deregister_script function takes the following parameter:

  • $handle (string), required. Name of the script to be removed.

Return Value

The wp_deregister_script function does not return any value.

Examples

How to Deregister a Default jQuery Script in WordPress

function custom_deregister_jquery() {
 if (!is_admin()) {
 wp_deregister_script('jquery');
 }
}
add_action('wp_enqueue_scripts', 'custom_deregister_jquery');

This snippet demonstrates how to deregister the default jQuery script in WordPress. The function custom_deregister_jquery checks if the current request is not an admin page using !is_admin(). If it is not an admin page, the wp_deregister_script function is called with the handle 'jquery' to remove the default jQuery script. The function is hooked to the wp_enqueue_scripts action to ensure it runs at the right time.

How to Deregister the Gutenberg Block Library CSS

function custom_deregister_gutenberg_block_css() {
 wp_deregister_script('wp-block-library');
}
add_action('wp_enqueue_scripts', 'custom_deregister_gutenberg_block_css', 100);

This snippet shows how to deregister the Gutenberg Block Library CSS in WordPress. The function custom_deregister_gutenberg_block_css calls wp_deregister_script with the handle 'wp-block-library' to remove the Gutenberg Block Library CSS. The function is hooked to the wp_enqueue_scripts action with a priority of 100 to ensure it runs after other scripts have been enqueued.

How to Deregister the WordPress Emoji Script

function custom_deregister_emoji_script() {
 wp_deregister_script('wp-emoji');
}
add_action('wp_print_scripts', 'custom_deregister_emoji_script');

This snippet explains how to deregister the WordPress Emoji script. The function custom_deregister_emoji_script calls wp_deregister_script with the handle 'wp-emoji' to remove the Emoji script. The function is hooked to the wp_print_scripts action to ensure it runs when scripts are printed.

Conclusion

The wp_deregister_script function is utilized to remove a previously registered script from WordPress’s queue. This function is particularly beneficial in scenarios where there is a need to prevent certain scripts from loading, either to avoid conflicts or to optimize page performance. By calling wp_deregister_script with the appropriate script handle, developers can ensure that specific scripts are not enqueued, thereby providing more control over the scripts that are loaded on a WordPress site. This function is a key aspect of managing script dependencies and maintaining a streamlined and efficient front-end environment.

Related WordPress Functions