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
The Benefits of Adding a Script in Your Head Tag
<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
src attribute blank.
Now, it’s important to understand the difference between
<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
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.
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.
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
As for the
<script> element, it simply prevents the markup from being sent to the browser until the code is actually needed.