WordPress comes with a bunch of default scripts that you can use to power your themes and plugins. jQuery is the commonly used script just like TinyMCE. But what if you want to integrate your own jQuery script?

We recently showed how to properly add jQuery scripts to your installation. WordPress uses the same internal mechanism, which means we can remove or even replace queued scripts with our own scripts.

In this tutorial, I will show you how unregister Existing scripts so you can add your own scripts. We will also introduce you to the different circumstances in which you can do it and tell you why some people think that removing an internal script is irresponsible.

Why modify the internal scripts: Case of jQuery?

There is a range of scenarios where you may want to modify the loaded internal scripts. For example, WordPress currently uses jQuery 1.11.3 by default, however, the latest version is actually 1.12.3 or 2.2.3. If you are writing an application that relies on advanced jQuery functionality, you might need to use a newer version of the script.

I also faced a situation where an old plugin was causing the issues with the outdated scripts. In this case, a customer was relying on a plugin, but the loaded script was not actually used. We thought removing it was the best option, but instead of directly modifying the plugin code which would have prevented future updates, we decided to use a new plugin to modify the loaded scripts.

It is important to note that while there are legitimate cases where you can remove and change the scripts loaded by the core of WordPress and other plugins or themes, you should always think twice and proceed with caution.

If you force a new version of jQuery on your Website, for example, it may cause unforeseen compatibility issues. You should always test WordPress locally if possible, and avoid modifying/removing scripts unless absolutely necessary.

Should we remove internal codes from WordPress

How to edit scripts

Thanks to the magic of WordPress, you can modify any script built into your theme or even create a plugin to do it for you. This means that you do not have to (and will not!) Change the original code that loads the offending script. Let me give you some examples.

Let's say your theme uses a script that you want to remove or replace. In this case, you can create a child theme and load the required code.

If you want to modify the scripts that come in the plugins, you can either make the necessary changes in your child theme or in a new plugin. Depending on the scope of the project, I recommend creating a plugin specifically to modify the scripts of other plugins.

If you want to edit a basic WordPress script, it's best to do it in the product you are writing. If you are working with a plugin you should do it on it, if it's a theme, you have to do it on the functions.php file.

The "deregistration" of Scripts

If you haven't already checked out the previous tutorial on how to properly add a script in WordPress, you can do so now and come back. We have seen that the method of adding scripts to WordPress is by registering them. Removing them is, of course, the reverse process, which means "deregistration" or rather deregistration. It's done using the wp_deregister_script () function like this:

wp_deregister_script ('jquery');

The only parameter you need here is the script namespace. The namespace is given when the script was saved and you can read on my post on adding jQuery to WordPress for more detailed information.

Using the code above will completely remove the jQuery script.

Replacing Scripts

If you want to replace a script, it is not enough to simply save it with new parameters (these will be ignored). You must unregister and then re-register the script. Here is a quick example:

function my_enqueued_assets () {wp_deregister_script ('jquery'); wp_enqueue_script ('script-name', '//code.jquery.com/jquery-2.2.3.min.js', array (), '2.2.3'); } add_action ('wp_enqueue_scripts', 'my_enqueued_assets');

Find saved scripts

If you're looking for all the scripts that WordPress saves, you're in luck. Have a look at the wp_enqueue_scripts documentation and you will find a table at your fingertips.

Finding scripts saved by plugins, however, is a bit more difficult. You can list all scripts in queue by dumping the contents of the $wp_scripts variable.

global $ wp_scripts; echo ' ';
var_dump ($ wp_scripts);
echo ' ';

Another method I use is to search for wp_enqueue_scripts in my plugins directory. This allows me to see which plugin is responsible for which script on my site.

Why Replacement Scripts is not 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 controversy in the WordPress community over whether it is actually appropriate to load it. make.

With the fact that WordPress loads jQuery in noConflict mode, there is very little chance that there will be a conflict, and therefore the themes should not change the behavior of WordPress, and especially WordPress updates the embedded version of jQuery regularly.

To summarize

While removing or replacing scripts, something you should not do every day, it is important to know how to do it correctly so that when you are faced with a situation where it is necessary, you can do it without worries and without “breaking” your blog.

By unsubscribing scripts via a custom plugin, you make sure you do not modify the source code of WordPress or a theme you are not the author of, which will allow you to keep your code safe during updates.