Javascript is a coding language that is slightly different to some coding languages; it’s not compiled at server level but instead on the client machine. It can be used for a huge range of purposes however it’s often used for front-end functionality purposes within WordPress.
In this tutorial we’re going to explore the different options available for adding Javascript to your WordPress site.
The way that you’ll add Javascript will often be based on the purpose of the Javascript being added for example if it’s for use with analytics software you’ll likely need to add it to the head of your site. However in other cases you may add it to the body or footer of your site.
One very important thing to remember is that you should almost never add Javascript directly to your original theme. There are multiple reasons for this the first being the way that WordPress compiles Javascript and the second being that if you’re not utilising a child-theme your code may be overwritten when you update your theme.
You also shouldn’t add your Javascript direct to the WordPress editor unless it’s a very small amount, in many cases this will slow your site down and isn’t a great way to manage writing out your Javascript.
Option 1 – Adding the code directly to your site files:
If you’re not already quite familiar with WordPress this is the most difficult method for adding Javascript to your site as it requires some general knowledge of working with FTP and or the file structure of your site as well as basic knowledge of php.
Initially you’ll want to create a file called whatever you want e.g. thisjavascriptfile.js this is where you’ll add your Javascript. You may want to add this to a folder such as /code or /scripts.
Then you’ll need to add your Javascript to your site, one of the easiest ways to do this is to add your Javascript direct to the head of the site.
For example:
<script type=”text/javascript” src=”/code/thisjavascriptfile.js”></script>
This will allow you to utilise the Javascript on all pages. How you add it to the header will depend on your current theme setup. In many cases your theme will have the option to add code to the header via a box in the theme settings.
Option 2 – Using Plugins for general Javascript:
There are some great WordPress plugins out there that will allow you to simply add your Javascript directly to them.
For example:
https://en-gb.wordpress.org/plugins/custom-css-js/
The above two plugins will allow you to add Javascript to your site via the plugins without the need to create a specific Javascript file and they include helpful tools such as syntax highlighting.
Option 3 Specific Plugins:
Using plugins for a specific purpose such as adding a Javascript analytics tag or implementing a website / crm integration.
In this case it’s generally best to use a plugin from the repository specific to your instance. Headers and footers is a great plugin and is good for analytics codes.
https://en-gb.wordpress.org/plugins/insert-headers-and-footers/
Other plugins may be specific to your purpose e.g. if you’re integrating a CRM they may have a plugin that helps you to do that in the repository.
Option 4 Theme Integration:
In many cases if you’re using a WordPress theme quite a large number of them come bundled with a Javascript integration panel within the theme options. Similar to the CSS options panel built into WordPress you can add custom Javascript to these and the theme manages the rest of the integration. They’re generally aimed at being used for analytics code integration and other small pieces of Javascript however depending on the theme that you’re using you can add quite a bit of code.
The major benefit to this is that you’re not installing an additional plugin however in many cases these boxes don’t include the additional features that a plugin has such as Syntax highlighting.
Further Reading:
Adding jQuery to a WordPress website
This is a relatively basic tutorial on how to implement Javascript into WordPress there is so much that you can do with it and the platform is incredibly versatile. We’d appreciate a share or any comments if you found this useful, thanks!








