Add JavaScript to WordPress: How to do It easily

People who use WordPress should learn more about JavaScript and how it is used for a website. To add JavaScript to WordPress, you need to find the most appropriate way to implement it for your own website.

Research holds great importance, as a wrong implementation of JavaScript could lead to consequences that will be troubling to fix.

Table of contents

What is a WordPress JavaScript?

A WordPress JavaScript is a way to add extra functions to a website without slowing it down. JavaScript represents a programming language which runs on the user’s browser. This type of programming offers developers the chance to add all sorts of functions to their sites, such as embedded video players, calculators or other services. The piece of JavaScript code simply needs to be copied and pasted into the website’s code.

Why you should add custom JavaScript in WordPress?

There are multiple reasons why you should add JavaScript to WordPress, but in order to do that, you must be aware of how the website is coded. When coding a WordPress website, there are three elements that may help you:

  • HTML
  • CSS
  • JavaScript

To create a very solid base for the WordPress site, you can use HTML and CSS. Even though they are great to lay the foundation of the website, they are limited in terms of altering the behavior of the site. When you want your site to act in a specific way, you can use JavaScript to do one of the following actions:

  • Changing, hiding or displaying certain HTML elements on the website
  • Adding values to CSS variables, as well as running operations that are more complex (merging variables or making an element interactable by clicking)
  • Changing the layout of the WordPress theme by adding another code block within it
  • Modifying a certain functionality of a WordPress plugin
  • Enhancing the website with content that is dynamic (animations, videos etc.)
  • Including an external element into the website (third-party APIs)

All these – and more – can be done by using JavaScript.

Using Insert Headers and Footers to add JavaScript

When adding JavaScript to WordPress, you need to simply copy and paste the block of JavaScript code to the website. This simple action adds a third-party tool to the website, be it a video player or other element. These code snippets have added either right under the head section or right before the </body> tag in the main code of the website. This influences where the element will be placed. To activate this plugin which is called Insert Headers and Footers, you must install it first.

In order to activate it after installation, you simply have to reach Setting and then the Insert Headers and Footers page. There are two different boxes that you can check – one for the header, one for the footer. Once you activated this plugin you can just paste the JavaScript code snippet and click on save. Because the plugin is activated, the code snippet you pasted will automatically load for all the pages of your website. This is the most basic knowledge you need to have to add JavaScript to WordPress.

Disabling WordPress filtering

When using JavaScript in WordPress, you should also disable blocking script tags from JavaScript. This setting can be found in wp-config.php, which is located in the root web directory. In order to enable custom tags, add this line of code there:

define( 'CUSTOM_TAGS', true );

Once you move to the functions.php page, copy and paste this code snippet in your base code:

function add_scriptfilter( $string ) {global $allowedtags;$allowedtags['script'] = array( 'src' => array () );return $string;}add_filter( 'pre_kses', 'add_scriptfilter' );

Extra tip: using this method means that you disable the security feature on your website, for any user permission level. Use this method only if you deeply trust your authors. Enabling the script tag is sometimes risky this way.

Learn how to Use Script n Styles plugin

You can add JavaScript to WordPress by using a plugin. Normally, you would have to load JavaScript within the <head> and </body> tags. In order to load JavaScript globally, a plugin can be used. A responsive one is Script n Styles. You can either include scrips on the entire website or include them at the page or even post level. You can also modify user permissions. Authors can publish posts and make the JavaScript active and contributors can save the posts without actually publishing them.

Use Advanced Custom Fields

If you wonder how to add JavaScript to WordPress the easiest, this plugin might be the solution. Once you install and activate the plugin, you can access the Custom Fields option under the administrative menu. Then, you can click on Add New. The purpose is to add a field that administrators can access and that can embed a JavaScript source file or JavaScript code within the tags.

  • The first step is naming the Field Group. This can be done by enabling a rule that displays this option if the user who logs in as an Administrator. This means that only the administrator can add these on the site.
  • The fields should be displayed beneath the content area, which means you select Standard when it comes to selecting the style of the Field Group. Set the formatting of the field to “Convert HTML into tags”. This will result in a good output, otherwise, it might translate badly.
  • Add the following code within header.php:

<?php the_field(‘header_script'); ?>

This should be added before </head>.

<?php the_field(‘footer_script'); ?>

This should be added before </body>.

  • Save the template files, update the page and click Publish.

The page will be published with the added JavaScript on it. The Advanced Custom Fields are an appropriate option if you want more functionality for users, pages that are already published and so on.

Hey, did you know data can be beautiful too?

wpDataTables can make it that way. There’s a good reason why it’s the #1 WordPress plugin for creating responsive tables and charts.

An actual example of wpDataTables in the wild

And it’s really easy to do something like this:

  1. You provide the table data
  2. Configure and customize it
  3. Publish it in a post or page

And it’s not just pretty, but also practical. You can make large tables with up to millions of rows, or you can use advanced filters and search, or you can go wild and make it editable.

“Yeah, but I just like Excel too much and there’s nothing like that on websites”. Yeah, there is. You can use conditional formatting like in Excel or Google Sheets.

Did I tell you you can create charts too with your data? And that’s only a small part. There are lots of other features for you.

FAQs about adding JavaScript to WordPress

1. What is the process for adding JavaScript to a WordPress site?

You have two options when adding JavaScript to a WordPress website: either straight into the header.php or footer.php files of the theme, or by using the wp_enqueue_script function. The preferred approach is to use wp_enqueue_script because it guarantees that the script will only be loaded when necessary and prevents conflicts with other scripts.

2. How can I add a JavaScript file to my WordPress theme?

The wp_enqueue_script function in your theme’s functions.php file can be used to include a JavaScript file in your WordPress theme. The script’s name, source URL, and any dependencies are passed to this method. The wp_head and wp_footer hooks can be used to load the script once it has been enqueued in the page’s header or footer.

3. Where do I add my JavaScript code in WordPress?

By making changes to the header.php and footer.php files in your theme, you can add JavaScript code to the header or footer of your WordPress website. As an alternative, you can add the script to the website using the wp_enqueue_script function, as previously mentioned.

4. Can I add inline JavaScript code to my WordPress pages and posts?

Yes, you may use the script element in the HTML editor to include inline JavaScript code into your WordPress pages and posts. Nevertheless, adding inline JavaScript code is typically not advised due to potential script clashes and performance issues.

5. How do I enqueue a JavaScript file in WordPress?

Use the wp_enqueue_script function in your theme’s functions.php file to enqueue a JavaScript file in WordPress. The script’s name, source URL, and any dependencies are passed to this method. The wp_head and wp_footer hooks can be used to load the script once it has been enqueued in the page’s header or footer.

6. What is the difference between wp_enqueue_script and wp_register_script in WordPress?

WordPress scripts are registered using the wp_enqueue_script function, whereas scripts are registered and enqueued using the wp_enqueue_script function. While wp enqueue script is used to load scripts that are always needed, wp_register_script is typically used when a script may be loaded conditionally.

7. How can I add a custom JavaScript function to a specific page or post in WordPress?

With the wp_enqueue_script function and the is_page or is_single conditional tags in your theme’s functions.php file, you may add a unique JavaScript function to a particular page or post in WordPress. This will guarantee that the script only loads on the posts or pages you specify.

8. Can I add JavaScript to a WordPress widget?

Yes, you can add JavaScript to a WordPress widget by building a custom widget and using the wp_enqueue_script function to add the script to the widget’s output. As an alternative, you can directly include the script in the widget’s HTML code.

9. How do I check if jQuery is already loaded in WordPress?

The wp_script_is function in your theme’s functions.php file can be used to determine whether jQuery is already loaded in WordPress. The script name, source URL, and handle used to register the script are passed to this function, which returns true if the script has already loaded.

10. How can I troubleshoot JavaScript errors on my WordPress site?

You can use the developer tools in your browser to go into the console for error messages and diagnose JavaScript issues on your WordPress website. To check for issues produced by WordPress or its plugins, you can also use a plugin like Query Monitor or Debug Bar. In order to determine if the error is brought on by a conflicting plugin or theme, you can also disable plugins and switch to the basic WordPress theme.

Ending thoughts on how to add JavaScript to WordPress 

These tips should be enough to teach you how to add JavaScript to WordPress in an easy way, without encountering any issues. After you do it once, the second time will be much easier and less time-consuming.

If you enjoyed reading this article on how to add JavaScript to WordPress, you should check out this one about how to reset WordPress.

We also wrote about a few related subjects like how to change WordPress URL, how to embed video in WordPress, how to edit HTML in WordPress, how to duplicate a page in WordPress, how to edit WordPress user roles and how to add WordPress featured image.

Sanja Pajic
Sanja Pajic

Full Stack Web Developer

Articles: 40