Need to add a script to your WordPress header? Whether it’s Google Analytics, Facebook Pixel, or some other tracking code, you’ve come to the right place. I’ll show you three different ways to add JavaScript to your WordPress site’s header – from the super-easy plugin method to the developer-friendly approach.
We’re not talking about customizing how your header looks or changing your site’s design. This is specifically about adding those code snippets that marketing tools, analytics platforms, and third-party services ask you to paste into your site. Let’s dive in!
Method 1: Using a Plugin (Easiest for Beginners)
Because we’re assuming that this task is being done by someone who’s not an expert on PHP or JavaScript development, we’ll start with using the Insert Headers and Footers plugin to add the script to the header of our page.
Here’s the basic process to add JavaScript(s) to your WordPress page headers:
- Log in to your WordPress dashboard and go to Plugins > Add New (You must be able to install plugins for this to work.)
- Search for the “Insert Headers and Footers”. Install and activate that plugin.
- Now we’ll use the plugin to add a script to our WordPress head. We’ll need to go to Settings > WP Headers and Footers in the left sidebar.
- Depending on whether your script needs to be added to your page header or footer, you’ll paste it into the appropriate box. Then press Save Changes.
With those steps, you’re done. You’re all set to get your WordPress header scripts running on the pages your visitors see. Hopefully that wasn’t too hard.
Method 2: Adding Script to Your Theme’s Header.php (Manual)
Note: This method only works with classic WordPress themes. If you’re using a modern block theme (FSE theme), you won’t have a traditional header.php file to edit. Learn more about the difference between classic and FSE themes.
If you prefer not to use a plugin, you can manually add scripts to your theme’s header file:
- Go to Appearance > Theme File Editor in your WordPress dashboard.
- Find and click on header.php in the right sidebar.
- Locate the
</head>closing tag. - Paste your script code just before the
</head>tag. - Click Update File to save your changes.
Warning: This method requires editing theme files directly. Always create a backup first, and be aware that your changes will be lost if you update your theme. For a more permanent solution, consider using a child theme.
Method 3: Using wp_enqueue_script() for Developers
If you’re developing your own plugins or themes for WordPress, the plugin method above isn’t the way you should add JavaScript to your WordPress pages. Instead, you’ll want to understand the more code-focused way to add header scripts in WordPress. That’s using a little function called wp_enqueue_script in WordPress PHP.
This method properly registers and loads your scripts following WordPress best practices. It handles dependencies, prevents conflicts, and ensures scripts load in the correct order.
We’ve got a great quick guide all about including JavaScript in WordPress.
Or if you want an even more thorough explanation, here’s our article about including JavaScript and CSS in WordPress. That article gives a little more theory and explanations of managing WordPress header and footer JavaScript insertions.
Which Method Should You Use?
- Use Method 1 (Plugin) if you’re a beginner or just need to quickly add tracking codes or third-party scripts
- Use Method 2 (Manual) if you want to avoid plugins and are comfortable editing theme files
- Use Method 3 (wp_enqueue_script) if you’re a developer building themes or plugins
For most WordPress users, the plugin method is the safest and easiest way to add scripts to your header without breaking anything.




