Adding the Dependency For JavaScript Files In WordPress

As the WordPress developer, we all know that, we can enqueue the required JavaScript and the CSS files through the WordPress function, wp_enqueue_scripts, within our themes or plugins being developed. So, it might come up in your head that, what about loading up the required files one by one? Is it required for all of them to be loaded one by one in your project? No, it is not required at all for your project, and one JavaScript file can be loaded by other JavaScript file via adding the dependency for JavaScript files, while enqueueing it via WordPress function.

From the above description, we now know that, WordPress has provided us the dependency function too, from which that file will be loaded as required from the another file enqueued, and hence do not need to be loaded again and again. So, here below is an example of adding the dependency for JavaScript files in WordPress.

function creative_blog_scripts() {
wp_enqueue_script('creative-blog-main-script', get_template_directory_uri() . '/js/creative-blog-custom.js', array('jquery'), null, true);

Here, as for the example above, we have added the required dependency for the jQuery file. Here, in the above example, firstly we have provided the handle name, which should be unique per the scripts as well as the CSS files to be enqueued. Now, after the handle has been added, then, we have provided the custom js file link, then, comes up the dependency file, ie, the handle of the main enqueued script file, which will be loaded since it is being called upon. So, if in case if you add the masonry handle instead of the jquery handle, then, jQuery will not be loaded and masonry will be loaded to be used instead of it.

If you have followed this tutorial properly and is success on applying the dependency for JavaScript files loading for your site, then, we can assume that you seem to be more familiar with this than ever before. But, if you are still confused on this, then, just leave us the message below in the comment box and we will follow it up.

Post navigation

Bishal Napit

Bishal Napit is a WordPress theme developer from Tansen, Palpa, with a passion to learn more on WordPress.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.