How to Add Script in the Head Tag of a Website that Uses WordPress

WordPress is a fairly popular content management system (CMS). In fact, it has almost as many downloads as Google’s own software, G Suite. Since its launch in 2005, it has been downloaded more than 300 million times and is the content management system used by 28% of the world’s most visited websites as of April 2019.

Now, imagine if I told you that WordPress supported adding scripts directly into its head tag. That’s right—the head tag. Not only does it support script elements, but it also supports the style element, enabling you to integrate external style sheets into your WordPress sites. In this article, I’ll walk you through the steps necessary to add a JavaScript object to your WordPress head tag.

The Benefits of Adding a Script in Your Head Tag

Adding a script in your head tag brings with it a number of advantages. To start with, JavaScript is mostly client-side code, which means your visitors don’t have to download or install additional software to use your content. Moreover, having JavaScript code in the head tag means your site will work for everyone, regardless of whether they’re using a typical desktop, tablet, or mobile device. Additionally, if you need additional functionality, such as hiding content based on a user’s browsing history or showing an image only when the user hovers their mouse over it, JavaScript is the perfect tool for the job. The <script> element simply prevents the markup from being sent to the browser until the code is actually needed.

Steps to Add Script

Now, let’s get down to the nitty-gritty and take a look at the steps necessary to add a script to your head tag.

To begin with, you’ll want to ensure that you have permission to edit the head tag of your websites. For that, you’ll need to log in to your WordPress dashboard and navigate to the Settings page. From there, you can either search for the head tag under the WordPress SEO section or look under the Appearance menu for the head tab. Once you’ve located the head tag, you can click on it to display the Edit HTML option.

Alternatively, you can click on the gear icon in the top right corner of the WordPress dashboard. From there, you can select Settings from the drop-down menu and navigate to the Permissions section. Here, you can enable the Edit Head option, which allows you to edit the head tag of your site.

When you’re ready to add a script to the head tag of your site, click on the Head tab and look for the script element. You’ll see that WordPress has already begun to style the HTML for your page, but has left a space for you to type in your own JavaScript code. Simply type in the JavaScript you’d like to use and leave the src attribute blank.

Now, it’s important to understand the difference between script and <script>. If you look at the example below, you’ll see that WordPress is styling the head tag with a script element. However, when you compare that to the <script> element, the latter is missing an src attribute.

As a technical matter, the script element is actually an HTML5 element. HTML5 is a standard that enables web developers to add functional and non-functional attributes to HTML elements in the body of a webpage. The HTML5 element was originally designed to provide authors with a way to add interactive elements to the head of their web pages, such as buttons that perform an action, like playing a sound file, or changing the color of a font based on the surrounding text.

Since this is a guest post, I’ll now invite you to try out this exciting new feature and see how simple it is to add a JavaScript object to your head tag.

What JavaScript Object Should You Use?

When choosing a JavaScript library to integrate into your WordPress site, you’re faced with a number of alternatives. For starters, you have the classic libraries, such as jQuery, which is a powerhouse in the space. jQuery is known for providing a variety of tools that can help you build more accessible and functional web pages. If that’s something that interests you, then you should consider using it.

On the other hand, perhaps you’re looking for something more basic. For that, you have the excellent YUI library, which stands for “yet another UI library”. As the name would suggest, YUI provides a variety of useful widgets and tools that can help you build more engaging web pages. If that’s the kind of thing you’re looking for, YUI could be the perfect fit.

It’s also important to keep in mind that not all JavaScript libraries are created equal. Some of them can be a lot more complicated to use than others, making the choice not solely based on the size of the community or how many stars they have on GitHub. For more information on how to choose a JavaScript library for your needs, check out this article by JavaScript ninja Chris Coyier.

The Final Step

Now that you have your script element in your head tag, it’s just a matter of adding a few closing tags and you’re all set. Simply add the following tags to the end of your body tag:

  • script language: JavaScript
  • src attachment:
  • /script

As for the <script> element, it simply prevents the markup from being sent to the browser until the code is actually needed.