How To Add A JS Library To WordPress (3 Ways)

By James LePage
 on June 28, 2021
Last modified on January 6th, 2022

How To Add A JS Library To WordPress (3 Ways)

By James LePage
 on June 28, 2021
Last modified on January 6th, 2022

If you're looking for a quick and easy way to insert JavaScript into WordPress, this article will discuss three different approaches to do just that. All of them can be done for free, and don't require any advanced knowledge.

Why Add A JS Library To WordPress?

JavaScript powers many of the advanced interactions and effects that you see on modern websites. There are hundreds of thousands of different library's that easily help you integrate unique aspects such as parallax, interactions, user input, client side processing, and more.

if you want inspiration about what you can do with JavaScript and WordPress, simply check out CodePen or Awwards to see some incredible examples and websites making use of Wor

If you want to add any of these to your website, you will need to add that specific JavaScript library to WordPress. Luckily, it's relatively easy to do, and this article will discuss three major ways -- you can choose which ever one is easiest to you.

JS Library/File: What To Expect

JS comes in several different shapes and sizes. Typically, it can be downloaded directly as a.JS file, but can also be included on a website by using a CDN or contact delivery network. Whatever the case is, it will typically be inserted into a website in one of two ways:

Using <script> and pointing to it's SRC. This method is used if you are hosting the JavaScript file on your server, or want to include a JavaScript library in WordPress from my contact delivery network.

<script type="text/javascript" src="/path/js-library.js"></script>Code language: HTML, XML (xml)

Inline Javascript. You can also add JavaScript in line to a word press website, this is typically not the best way to do things if you are inserting a complete library. However, if you have a small JavaScript snippet that you want to add to WordPress, this would typically be how it's done:

<script type="text/javascript"> 
// your code
</script>Code language: HTML, XML (xml)

Examples would include tracking snippets, like the ones that Google Analytics or GTM have.

For any website, even if it isn't built with WordPress, when inserting a JavaScript library, you would add it to either the header or the footer. Typically, JavaScript is inserted directly into the footer, so it doesn't interfere with the rendering process of the webpage. This leads to quicker loading times, and higher ranking on Google.

Here's a real world example of how JS is inserted into any website, from The New York Times.

See all the script elements?

A WordPress website is no different, JavaScript is inserted the same way. So now let's take a look at three different methods you can use to add a JavaScript library to your word press website. These methods will vary in difficulty, listed in order of easiest to most complex. Each has it's own best use case.

Method 1: Use A Plugin To Add A JS Library To WordPress

Probably the easiest way to add a JavaScript library to WordPress is to use a prebuilt plugin designed to specifically insert JavaScript into the header or the footer of your site. The most popular plugin to do this is called (unsurprisingly) Insert Headers and Footers by WPBeginner. It has 1 million + active installations.

To use it, install and activate the plugin on your website, and then simply copy and paste your JavaScript into one of the three fields (html).

This plugin allows you to add any HTML into the header, body, or footer of your site. However, it's a bit simple and if you're looking for more control, consider using method two.

Method 2: Use A Script Manager To Add Javascript To WordPress

Method two is using a script manager to add and manage JavaScript libraries and files on your WordPress website. The free (and best known) script manager to use for this is Code Snippets. Here, you can add various snippets of JavaScript, including those script tags talked about above, and manage them from a dashboard on the back end of your website. This makes it very easy to toggle things on and off, and you can also dictate where the scripts and up by using PHP (ie, insert into the header or the footer of your website.

Code Snippets – WordPress plugin |

To insert the JS into a WordPress website's header, use this code:

add_action( 'wp_head', function () { ?>

<script src=""></script>
//this is the link to your CDN or self hosted JS file 👆
    Inline JS Goes Here

<?php } );Code language: JavaScript (javascript)

To insert the JS into a WordPress website's footer, use this code:

add_action( 'wp_footer', function () { ?>

<script src=""></script>
//this is the link to your CDN or self hosted JS file 👆
    Inline JS Goes Here

<?php } );Code language: JavaScript (javascript)

There are also two popular premium script manager plugins that you can use as well. The difference between just using code snippets and these two premium plugins is that they offer a much easier to use interface, and additional tools that help you manage any and all code and a word press website. As a professional WordPress development agency, this is our recommended and go to method when building out websites.

Use Advanced Scripts to add and manage JS on a WordPress Website

Both of these premium plug-ins make it easy to toggle javascript on and off on the WP site. Unlike code snippets, there are pre-built settings that allow you to insert directly into whatever WordPress hook you want, which could include the header or the footer for the main website, admin area, login page, and more. You can also generate short codes, meaning that you can insert JavaScript libraries and files into any area of a word press website by simply inserting the short code generated.

Read the full review of Advanced Scripts here.

Use Scripts Organizer to add and manage JavaScript on a WP site

Read the full review of Scripts Organizer here.

We own and use both of these plugins on various projects. Script Organizer is the best when you are looking to schedule various scripts for specific times or ranges. Advanced Scripts is great if you want to conditionally insert scripts (you can use standard PHP or preset rules to do this). Both are very well coded, have quality development teams behind them, and get our seal of approval. Each has a 14 day moneyback guarantees so you can try both of them, and see which works best for your use case or situation. Finally, unlike many WordPress plug-ins, these tools are sold as lifetime deals, meaning that there will be no annual renewal needed, and you still receive unlimited updates for the lifetime of the product.

Method 3: Add JS Directly To Your Theme or Make A Custom Plugin

You can also make your own word press plug-in which does essentially what method to is doing, except instead of inserting a code snippet from the back end of a word press website, you're doing it from a plug-in. This may lead to more performance, security, and make it tougher for a client to break.

But, at the same time if you are inserting a entire JS library, it's up to you the developer to update the code within the plug-in whenever the library itself receives an update. There's no easy way to make this automatic, but it must be done for security purposes.

If you want to add JavaScript to a word press website by creating your own plug-in, there's an easy boiler plate called My Custom Functionality.

Simply download it, upload the JavaScript to the JavaScript folder, and then enqueue the code via plugin.php file.

There are comments and examples already here, so all you need to do is uncomment it, change the file name to the one that you have uploaded into the JS folder, zip the plug-in, upload it to a word press website, and enjoy the JavaScript library that has been added via this method.

If you want to add the JavaScript directly to your word press theme, you can do so by using the code snippet found above, and adding them directly to the functions.php file. This makes it a bit more difficult to manage as there's no graphical interface for you to see what code is being run on your website, and if you're not using a child team, this will be overwritten whenever the theme is updated.

These three methods of adding JavaScript to a WordPress website should have you covered for any use case or scenario. As you can see, it's pretty a pretty simple endeavor to add JavaScript to a website, and WordPress probably makes it even easier to manage than if you were hard coding it.

Performance Considerations

By nature, JavaScript is a render blocking resource, meaning that it will delay the loading of a webpage if you don't do for it or load it asynchronously. Our recommendation is to always insert JavaScript libraries into the footer of a word press website, and the lady initialization until a user actually needs it. That way, the page isn't bloated, and there is no performance impact. Google really hates render blocking resources and will penalize your website in terms of ranking if JS makes it slow.

The easiest way to make sure that the JavaScript you have inserted on a word press website is performing is to use a plug-in such as WPRocket to fully optimize and and all JS on the website.

Read these guides to learn more:

Wrapping Up

As an agency, we will typically make our own custom plugins to house JavaScript, or use a script manager as we primarily develop with Oxygen Builder (it disables all theme functions, including the functions.php file). However, if you're a standard user of WordPress, we recommend method one or method two... Both make it very easy to manage any and all JavaScript on a word press website, which is important if you are creating it yourself, and need to debug. By having a graphical user interface showing you what JavaScript is running on your site at a specific moment, you can easily toggle it on and off, fixing any issues that clashing libraries and plug-ins may have.

If you have any questions about how to add a JavaScript library or file to a WordPress website, and what the best way to do it is, feel free to reach out in the comment section below. We hope this article was helpful, and if you have any tips of your own, leave a comment!

Subscribe & Share
If you liked this content, subscribe for our monthly roundup of WordPress news, website inspiration, exclusive deals and interesting articles.
Unsubscribe at any time. We do not spam and will never sell or share your email.
Notify of
Inline Feedbacks
View all comments
Article By
James LePage
James LePage is the founder of Isotropic, a WordPress education company and digital agency. He is also the founder of, a venture backed startup bringing AI to WordPress creators.
We're looking for new authors. Explore Isotropic Jobs.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram