How to add custom code to a WordPress site
WordPress has no built-in box for head scripts. On a self-hosted site you either use a header and footer plugin or hook into wp_head from code. WordPress.com works the same way through a plugin, once the site is on a plan that permits script tags.
Before you start
- Self-hosted: an administrator account. There is no plan requirement.
- WordPress.com: a plan that allows plugins, with hosting features activated. Free sites cannot use <script> tags.
- The script from your tool, as a complete <script> tag
- A theme that calls wp_head(), which is what prints head scripts on the front end
Steps
- Check what your site allowsA self-hosted site can do this today. On WordPress.com, look at your plan first: script tags need a plugin-enabled plan and activated hosting features. WordPress.com’s own pages name the qualifying plans differently, so go by its current plan page.
- Install a header and footer pluginWPCode is the one WordPress.com’s own guide uses. Install and activate it. The route is identical on self-hosted WordPress.
- Paste the script into the Header boxGo to Code Snippets → Header & Footer. Paste the whole tag into the Header box and click Save Changes.
<script defer src="https://example.com/tool.js"></script>An example tag. Use the one from your tool. - Or print it from code with wp_headIf you would rather not add a plugin on a self-hosted site, hook the wp_head action. It prints into the head on the front end, and only when the theme calls wp_head().
add_action( 'wp_head', function () { echo '<script defer src="https://example.com/tool.js"></script>'; } );PHP for a small plugin or your theme’s functions.php.
Check it worked
- Log out, or open the site in a private window, then view the page source and search for the script’s address.
- Reload with developer tools open on the Network tab and confirm the script is requested.
What goes wrong
- WordPress.com removes the script tag
- The site is on a free plan or one without plugins. Move to a plan that allows plugins, activate hosting features, then add the script through WPCode.
- The wp_head code prints nothing
- The action fires only if the theme calls wp_head(). Look in the theme’s header template for that call, or switch to the plugin route.
- The tag is in the source but the script loads late or never
- A caching or optimisation plugin may be delaying, combining or stripping third-party scripts. Check its settings and exclude this script’s address.
Checked on 2026-11-11 against WordPress Developer Resources: wp_head hook, WordPress.com Support: Add code to headers, WordPress.com Support: Use code on your site.
Increase conversions on your WordPress site by 20%
Dynamite personalizes the page for every visitor. Dynamite personalizes a WordPress site from one script tag in the head, added through either route above. It runs in the visitor’s browser, so check that any optimisation plugin leaves the tag alone.
Dynamite for WordPressQuestions
Can I add a script tag on a free WordPress.com site?
No. WordPress.com says free sites cannot use script tags. You need a plan that allows plugins, with hosting features activated.
Do I need a plugin on self-hosted WordPress?
No. A few lines hooked to wp_head do the same job. A header plugin is the route that needs no PHP.
How do I remove the script?
Clear it from the Header box and click Save Changes, or delete the wp_head code you added.