Developer · Guide
How to Add JavaScript to WordPress (4 Safe Methods)

Short answer
To add JavaScript to WordPress, use a Custom HTML block for a script on one page, a header and footer plugin for site-wide tags such as analytics, or wp_enqueue_script in a small plugin or child theme for your own script files. Load scripts with defer or in the footer so they do not slow the page down.
Key takeaways
- Choose the method by scope: one page, every page, or your own script files.
- Third-party tags like analytics and chat widgets usually go in the site header or footer through a plugin.
- Your own JavaScript files should be loaded with wp_enqueue_script, which handles order and dependencies.
- Use the defer or async loading strategy so scripts do not block the page from rendering.
- Only users with the unfiltered_html capability can save script tags in content.
Which method should you use to add JavaScript?
Pick the method by where the script needs to run and who wrote it: a single page, every page, or your own script files that need proper loading.
| Script | Best method |
|---|---|
| A widget or embed code for one page | Custom HTML block on that page |
| Analytics, pixels, chat or verification tags on every page | Header and footer plugin |
| Your own .js file for the whole site | wp_enqueue_script in a plugin or child theme |
| A few lines that configure another script | wp_add_inline_script |
| JavaScript for the block editor itself | A plugin that enqueues editor assets |
For other kinds of code, such as PHP snippets or CSS, see our wider guide on adding code to WordPress.
How do you add JavaScript to a single page?
Add a Custom HTML block to the page, paste the script tag and any HTML it needs, then preview and update the page.
This suits embed codes from booking tools, calculators, maps or forms that only one page uses. The script runs only where the block is placed.
Only users with the unfiltered_html capability can save script tags. On single sites that means Administrators and Editors by default. The Roles and Capabilities documentation explains who has it.
Avoid pasting long custom scripts into content. They are hard to maintain, can break when someone edits the page, and are easy to lose in a redesign.
How do you add JavaScript to every page?
Use a header and footer plugin such as WPCode, paste the script into the header or footer box the vendor specifies, and save.
The free WPCode plugin adds header, body and footer boxes under its settings. Many SEO and analytics plugins also have fields for tracking codes.
Prefer the footer when the vendor allows it. Scripts in the header can delay the page from appearing, while footer scripts load after the content.
Keep a note of every tag you add, when you added it and why. Sites slowly collect old tracking scripts that nobody uses but that still slow every page down.
How do you enqueue your own JavaScript file properly?
Place your .js file in a plugin or child theme and load it with wp_enqueue_script on the wp_enqueue_scripts hook, choosing defer or async loading.
This is the method WordPress recommends for theme and plugin developers, and it is what well-built plugins do too. It avoids loading the same library twice, respects dependencies such as jQuery, and works with caching and optimization plugins.
A minimal example in a small plugin:
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_script(
'site-custom',
plugins_url( 'js/site.js', __FILE__ ),
array(),
'1.0.0',
array( 'strategy' => 'defer', 'in_footer' => true )
);
} );
The wp_enqueue_script reference documents each parameter, including the loading strategy option. Our full guide on enqueueing scripts and styles covers conditional loading and versioning.
Change the version number when you edit the file. WordPress adds it to the URL, which makes browsers and caches download the new file.
How do you add a small inline script?
Use wp_add_inline_script to attach a few lines of JavaScript to a script you already enqueue, instead of echoing script tags into the page.
Inline scripts are useful for configuration, such as passing a setting to a library. Attaching them to a registered script keeps them in the right order.
The wp_add_inline_script reference shows how to place code before or after the main script.
For passing data from PHP to JavaScript, many developers use wp_add_inline_script with a JSON object rather than older techniques.
How do you load a script only on the pages that need it?
Wrap the enqueue call in a conditional check, such as is_page, is_singular or is_front_page, so the script loads only where it is used.
Loading every script on every page is one of the most common causes of slow WordPress sites. A booking widget needed on one page should not load on the blog.
For example, to load a script only on a page with the slug “contact”:
add_action( 'wp_enqueue_scripts', function () {
if ( is_page( 'contact' ) ) {
wp_enqueue_script( 'contact-map', plugins_url( 'js/map.js', __FILE__ ), array(), '1.0.0', array( 'strategy' => 'defer' ) );
}
} );
Other useful checks include is_singular( 'product' ) for store product pages, is_front_page() for the homepage and has_block() to load a script only when a page uses a particular block.
Header and footer plugins often offer the same control without code. Look for page rules or conditional logic in the snippet settings.
How do you add JavaScript to the block editor?
Use the enqueue_block_editor_assets hook in a plugin to load scripts inside the editor, which is how custom blocks, editor panels and editor tweaks are added.
Front-end scripts and editor scripts are separate. A script enqueued on wp_enqueue_scripts does not run inside the editor, and editor scripts do not load for visitors.
Modern block development uses a build step and block.json to register editor and view scripts automatically. Our guide to creating a custom block walks through that setup.
For a quick editor tweak, such as unregistering a block style, a small script enqueued on enqueue_block_editor_assets is enough.
How do you stop JavaScript from slowing down your site?
Load scripts with defer or async, only on pages that need them, and remove scripts you no longer use, because JavaScript is often the biggest cause of slow interaction on WordPress sites.
The defer attribute downloads a script without blocking the page and runs it after the HTML is parsed. The async attribute runs it as soon as it arrives. MDN explains both in its guide to the script element.
Third-party scripts deserve extra care. Chat widgets, ad tags and social embeds can add a lot of work for the browser. The web.dev guide to loading third-party JavaScript lists ways to reduce their cost.
Our guide on deferring JavaScript shows the settings and code for each method.
To find scripts that load but do little, see our guide to reducing unused JavaScript.
Does jQuery still come with WordPress?
Yes, WordPress still ships jQuery for compatibility, but new code does not need it; modern browsers support the same features in plain JavaScript.
If your script depends on jQuery, list it as a dependency when enqueuing so WordPress loads it first. If it does not, leave jQuery out so pages that do not need it can skip loading it.
Many themes and plugins have moved away from jQuery to keep pages lighter, and WordPress core's own newer features are written without it. Writing new code in plain JavaScript keeps your site ready for that.
How do you keep added JavaScript secure?
Only add scripts from sources you trust, keep the number of external scripts small, and avoid giving content editors the ability to add raw script tags.
Every external script can read and change your pages. A compromised third-party script can inject spam or steal form data, so each one is a trust decision.
A few safeguards:
- Load scripts over HTTPS from the vendor's official URL
- Remove tags from services you have stopped using
- Limit Administrator accounts, since they can add scripts
- Consider a Content Security Policy that lists allowed script sources
Our guide on adding a Content Security Policy to WordPress shows how to set one up without breaking your site.
How do you check that your script works?
Open the page, press F12, check the Console for errors and the Network tab to confirm the script loaded, then test the feature it adds.
If nothing happens, look for a red error mentioning your file. Common causes are a wrong file path, a missing dependency or code that runs before the page element exists. Wrapping your code in a DOMContentLoaded listener, or loading it with defer, fixes the last one.
Caching and optimization plugins can combine or delay scripts. If your script works with optimization off and fails with it on, exclude your file in the plugin's JavaScript settings.
Frequently asked questions
Where do I put JavaScript in WordPress?
Put one-page scripts in a Custom HTML block, site-wide tags in a header and footer plugin, and your own script files in a plugin or child theme loaded with wp_enqueue_script.
Can I add JavaScript to WordPress without a plugin?
Yes. Use a Custom HTML block for one page, or enqueue a script file from a child theme's functions.php. A small site-specific plugin is better than a child theme for scripts that should survive a theme change.
Why does WordPress remove my script tags?
WordPress strips script tags from content for users without the unfiltered_html capability. Ask an administrator to add the script, or use a header and footer plugin managed by an administrator.
Should JavaScript go in the header or footer in WordPress?
Use the footer or the defer strategy whenever possible, so the page content appears first. Only put scripts in the header when a vendor requires it, such as some consent or testing tools.
How do I add JavaScript to only one page?
Use a Custom HTML block on that page, or enqueue the script conditionally in PHP with a check such as is_page for that page's slug or ID.
Sources
- wp_enqueue_script() WordPress Code Reference
- wp_add_inline_script() WordPress Code Reference
- The Script element MDN Web Docs
- Loading third-party JavaScript web.dev
- Roles and Capabilities WordPress.org Documentation
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.
Related guides
About the editors
Researched from the primary sources listed above and checked against our editorial rules before publishing.


