Include Builtin scripts from WordPress using wp_enqueue_script()

Since we already showed you how to Include Javascript and CSS files through your WordPress Plugin, this post will be about how to include builtin scripts from WordPress. Why would we use these pre-registered scripts?

Two Reasons Why to use Builtin scripts from WordPress

There are two main reasons why using the pre-registered scripts is beneficial. First, this method will help to prevent jQuery conflicts. Second, it is a lot easier to use dependencies. Here is a list of all default scripts included and registered by WordPress. First lets look at an example to help prevent jQuery conflicts: if your plugin uses jQuery 1.9.2 and WordPress uses jQuery 1.11 then some features, like the drag and drop, might not work. So instead of doing this:

wp_enqueue_script( ‘my-plugin-jquery’,'//');

Do this:

wp_enqueue_script( ‘jquery’);

With this code you are sure that there will be no jQuery conflicts. Dependencies are easily added too. For example, if you have your own javascript file that depends on jQuery, then do this:

wp_enqueue_script( ‘my-plugin-js', plugins_url( 'include/js/my_plugin.js', __FILE__ ), array(‘jquery'));

The third argument is where the dependencies are added. Also, its an array so you can have multiple dependencies. Overall, its best to use this already registered scripts to have less conflicts and to easily add dependencies

Leave a Reply

Your email address will not be published. Required fields are marked *