Is Replacing Default WordPress Scripts Like jQuery a Good Idea? (and How to Do It)
WordPress comes with a whole bunch of default scripts out-of-the-box that you can take advantage of to power your themes and plugins. jQuery is one commonly used script, as is TinyMCE. But what if you want to replace a built-in script with your own?
We recently looked at how to add jQuery scripts properly to your install. WordPress uses the same mechanism internally and in plugins, which means we can remove or even replace enqueued scripts with our own.
In this article, I’ll show you how to deregister existing scripts so you can enqueue your own. We’ll also walk through why you might want to do it – and why some people believe replacing scripts is irresponsible.
Why Modify Scripts?
There are a couple of scenarios where you might want to change the loaded scripts. For example, WordPress currently loads jQuery 1.11.3 by default, however, the latest version is actually 1.12.3 or 2.2.3. If you are writing an app that relies on advanced jQuery functionality, you might need to use a newer version of the scripting language.
I’ve also run into a situation where an old plugin was causing issues with outdated scripts. In this case, a client was relying on a plugin but the loaded script wasn’t actually being used. We felt removing it was the best option, but instead of modifying the plugin’s code directly, which would have prevented future updates, we decided to use a new plugin to change the loaded scripts.
It’s important to note that while there are legitimate cases for removing and changing scripts loaded by WordPress core and other plugins and themes, you should always think twice and proceed with caution.
If you force a newer version of jQuery on your website, for example, it may cause unforeseen issues. You should always test on a locally hosted WordPress install if possible, and avoid changing/removing scripts if it’s not absolutely necessary.
How to Modify Scripts
Through the magic of WordPress, you can modify any built-in script from your theme or even create a plugin to do it for you. This means that you don’t have to (and shouldn’t!) modify the original product that loads the offending script. Let me give you a couple of examples.
Let’s say your theme uses a script you want to remove or replace. In this case, you could create a child theme and apply the required code there.
If you want to modify scripts that originate within plugins, you could either make the necessary changes in your theme/child theme or in a new plugin. Depending on the project scope, I would recommend creating a plugin specifically to modify scripts from other plugins. That way, switching this functionality on and off is trivial.
If you want to modify a WordPress core script, it’s best to do it within the product you are writing. If you’re working with a plugin you should do it there, and if you are developing a theme use your functions.php file.
If you haven’t already checked out my post on how to properly add jQuery to WordPress, check it out now and come back. We looked at the method of adding scripts to WordPress is to register them. Removing them is, naturally, the opposite process, which means it is called deregistering. It is done using the
wp_deregister_script() function like so:
The only parameter you need here is the handle of the script. The handle was given when the script was registered and you can read over my post about adding jQuery to WordPress for more detailed information.
Using the code above will completely remove the jQuery script from all pages of your install.
If you want to replace a script it is not enough to just register it with new parameters – these will be ignored. You need to deregister it and then re-register it. Here’s a quick and straight forward example:
Finding Registered Scripts
If you’re looking for all the scripts that WordPress registers, you are in luck. Take a look at the wp_enqueue_scripts function documentation and you’ll find a handy table there.
Finding scripts registered by plugins, however, is a bit more difficult. You can list all enqueued scripts by dumping the contents of the
Another method I use is to search the contents of my whole plugins directory for
wp_enqueue_scripts. This allows me to see which plugin is responsible for which script on my site.
Why Replacing Scripts Isn’t Always a Good Idea
While some theme and plugin developers might find it absolutely necessary to load their own scripts, and in particular a different version of jQuery, there is some contention in the WordPress community around whether it’s actually appropriate to do so.
The fact that WordPress loads jQuery in noConflict mode, there’s the possibility of increasing the support load for other developers, that themes should not modify core WordPress behavior, and that WordPress actually updates the bundled jQuery version frequently, are just some reasons developer Pippin Williamson has argued against messing with the default jQuery script.
While removing or replacing scripts is not something you need to do every day, it’s important you know how to do it properly so that when you are faced with a situation where it is necessary you can do it correctly.
By properly deregistering and re-registering scripts you make sure your WordPress install not only remains modular but it also easy to modify.
Have you bumped into any situations where it was necessary to deregister a script, like jQuery? Let us know in the comments below.Tags: