Buy All 47 Premium Themes For the Price Of $199.99

Custom JS – Add JavaScript Anywhere in WordPress

Analytics, tag managers, pixels, chat widgets or your own scripts – add them to WordPress in seconds, without editing theme files and without losing them when the theme updates.

Custom JS 2.0 – add JavaScript anywhere in WordPress

Download Custom JS for free from WordPress.org

Features

  • Head, Body and Footer – three places for site-wide code, including right after the opening <body> tag.
  • Unlimited snippets – named pieces of code that you can switch on and off, duplicate, search and add notes to.
  • Conditions – load a snippet only on the front page, posts, pages, archives, search or 404 pages, on chosen posts, or on URLs that match a rule.
  • Visitor targeting – everyone, logged-in or logged-out visitors, everyone except administrators, chosen user roles, desktop or mobile.
  • Scheduling – a start and end date for campaigns and promotions.
  • External scripts – add a script by URL with async, defer or type="module".
  • Auto-wrap – paste plain JavaScript and the <script> tags are added for you. Optionally run the code after the page has loaded.
  • jQuery on demand – load jQuery only when a snippet needs it.
  • Admin area and login page code.
  • Per-page code – a Custom JS box on the edit screen of posts and pages.
  • Code editor – syntax highlighting, line numbers, error checking, dark theme and Ctrl+S / ⌘+S to save.
  • Revisions – every save is kept and can be restored with one click.
  • Import and Export – back up everything or move it to another site as a JSON file.
  • Safe mode – switch off all custom code instantly if something breaks.
  • Secure and lightweight – no tracking, no external requests, security checks on every action.

Screenshots

Site Code screen with Head, Body and Footer code editors
Site Code – Head, Body and Footer code with syntax highlighting and an on/off switch for each.
List of snippets with status, location, priority and conditions
Snippets – every snippet with its status, location, priority and conditions at a glance.
Snippet editor with code, location, visitor and schedule options
Snippet editor – code, location, priority, page rules, visitors, device and schedule.
Custom JS box on the page edit screen
Per-page code – JavaScript that loads only on the page you are editing.
Revisions screen with saved versions of the code
Revisions – see what changed and restore any earlier version.
Import and Export screen
Import / Export – back up your code or move it to another site.
Settings screen
Settings – kill switch, auto-wrap, hide code for administrators, editor options and more.

Installation

From your WordPress dashboard

  1. Go to Plugins → Add New.
  2. Search for Custom JS.
  3. Click Install Now, then Activate.

From a ZIP file

  1. Go to Plugins → Add New → Upload Plugin.
  2. Choose the custom-js.zip file and click Install Now.
  3. Click Activate.

Requirements

  • WordPress 4.6 or newer (the code editor needs WordPress 4.9+, the Body location needs WordPress 5.2+).
  • PHP 5.6 or newer.

Updating from version 1.0? Just update. Your existing Head and Footer code stays where it was and is printed exactly as before.

Quick start

  1. Open Custom JS in the admin menu.
  2. Paste your code into Add JS in Head or Add JS in Footer.
  3. Click Save Changes or press Ctrl+S.
  4. Open your site and view the page source to see your code.

Need the code only on some pages, or only for some visitors? Use a snippet instead.

Site Code: Head, Body and Footer

The Site Code tab holds code that loads on every page of your site.

Field Where the code is printed Good for
Add JS in Head Inside <head> Google Analytics, Google Tag Manager, site verification, preconnect links
Add JS after <body> Right after the opening <body> tag Google Tag Manager <noscript>, banners that must appear first
Add JS in Footer Before </body> Most scripts – they do not slow down page rendering

Each field has its own Enabled switch, so you can turn code off without deleting it.

Body location: this uses the wp_body_open hook. All modern themes support it. If your code does not appear, your theme is older – use the Footer instead.

Auto-wrap

When Auto-wrap is on (Settings tab), plain JavaScript is wrapped in <script> tags for you. So you can paste this:

console.log('Hello!');

and the site will print this:

<script>
console.log('Hello!');
</script>

Code that already contains HTML tags (for example a copied Google tag) is never changed. Auto-wrap is on for new installs and off for sites updated from version 1.0, so their output stays exactly the same.

Snippets

A snippet is a separate, named piece of code with its own settings. Use snippets when you have more than one script, or when a script should load only in some places.

Create a snippet

  1. Go to Custom JS → Snippets and click Add New Snippet.
  2. Enter a title, for example Facebook Pixel.
  3. Choose Code and paste your code, or choose External script URL and enter the script address.
  4. Choose a Location and, if needed, set conditions.
  5. Click Save Snippet.

Locations

Location Where the code is printed
Site – Head Inside <head> on the front end
Site – After <body> Right after the opening <body> tag
Site – Footer Before </body> on the front end
Admin – Head / Footer On every dashboard page
Login – Head / Footer On the login page (wp-login.php)

Code options

Wrapping
Auto wraps plain JavaScript in <script> tags and leaves HTML alone. Always wrap always adds the tags. Raw HTML prints the code exactly as entered.
Run after the page has loaded
Runs the code on DOMContentLoaded, so it can safely use elements anywhere on the page.
This code needs jQuery
Loads jQuery on pages where the snippet runs, even if the theme does not use it.

External scripts

Choose External script URL, enter the address and pick how it should load:

  • Normal – loads and runs immediately.
  • async – loads in the background and runs as soon as it is ready. Good for independent scripts like analytics.
  • defer – loads in the background and runs after the page is parsed. Good for most widgets.
  • type=”module” – for modern ES module scripts.

Priority

When several snippets share a location, the one with the lower number is printed first. The default is 10.

Managing snippets

  • Click Active / Inactive in the list to switch a snippet on or off.
  • Hover a snippet to Edit, Duplicate or Delete it. A duplicate starts as inactive.
  • Use the Filter snippets box to find a snippet by title, notes or location.

Conditions

Conditions decide where and for whom a snippet loads. With no conditions, a snippet loads everywhere.

Where to show

Rule Matches
Front page The home page of the site
Blog (posts) page The page that lists your latest posts
Single posts Any blog post
Pages Any page
Any single post type Posts, pages, products and any other single item
Archives Category, tag, date and author pages
Search results The search results page
404 page The “page not found” page
Only on posts/pages IDs or slugs, for example 12, 45, contact
URL contains Addresses that contain the text, one rule per line
Exclude posts/pages Never load on these IDs or slugs
Exclude URLs containing Never load on these addresses

How rules combine: if you choose several rules, the snippet loads when any of them matches. Exclusions always win.

URL rules

A plain rule matches any address that contains the text. Add * to match the full path with a wildcard.

Rule Matches
/shop/ /shop/, /shop/shoes/, /en/shop/
/blog/*/amp /blog/my-post/amp
*?utm_source=* Any address that has a utm_source parameter

Visitors and device

  • Everyone, Only logged-out visitors, Only logged-in users or Everyone except administrators.
  • Limit to user roles – for example only Editors or only Customers.
  • Device – all devices, desktop only, or mobile and tablet only.

Note: device and visitor rules are checked when the page is built. If you use full-page caching, the cached page may be shown to everyone. Exclude those pages from the cache or use the rules without caching.

Schedule

Set a Start and/or End date and time. The snippet loads only between them, using your site’s time zone (Settings → General). Perfect for Black Friday banners, countdowns and limited-time offers.

Admin and Login code

The Admin & Login tab adds code to the WordPress dashboard and to the login page – for example to customise the login screen or add a help widget for your editors.

Each area has its own on/off switch. If admin code breaks the dashboard, see Safe mode.

Code for a single page

On the edit screen of posts and pages you will find a Custom JS for this page box with two fields:

  • Head – printed inside <head> of this page only.
  • Footer – printed before </body> of this page only.

Tick Disable the global Custom JS code and snippets on this page to keep this page free of all other custom code, for example on a landing page or a checkout page.

Choose which post types show the box in Custom JS → Settings → Per-page code.

Revisions

Every time you save the Site Code or Admin & Login tab, a copy is kept. In the Revisions tab you can:

  • open any revision to see its code;
  • see which fields differ from the current code;
  • click Restore this revision – your current code is saved as a new revision first, so nothing is lost.

The last 15 revisions are kept by default. Change the number in Settings.

Import and Export

Export

Go to Import / Export and click Download Export File. You get a JSON file with all code, snippets and settings.

Import

  1. Choose the JSON file, or paste its content into the text box.
  2. Choose what to import: code, snippets and/or settings.
  3. Choose to add the snippets to your existing ones or to replace them.
  4. Click Import.

Your current code is saved as a revision before the import.

Settings

Option What it does
Switch off ALL custom code A kill switch. Nothing is printed anywhere; your code is kept.
Auto-wrap plain JavaScript Adds <script> tags to code that has no HTML tags.
Do not load site code for administrators Keeps your own visits out of analytics.
Do not load site code for any logged-in user Same as above, for all logged-in users.
HTML comment markers Adds <!-- Custom JS: … --> around printed code, so you can find it in the page source.
Use the code editor Syntax highlighting, line numbers and error checking.
Dark editor theme A dark colour scheme for the code editor.
Per-page code Shows the Custom JS box on the edit screen of the chosen post types.
Revisions to keep How many revisions are stored (1–100).
Delete all data when the plugin is deleted Off by default, so your code is safe if you reinstall.

Safe mode and troubleshooting

A JavaScript error can break a page or even the dashboard. Custom JS gives you three ways out:

  1. For one page: add ?cjs_safe_mode=1 to the address while logged in as an administrator, for example https://example.com/wp-admin/?cjs_safe_mode=1. That page loads without any custom code.
  2. For the whole site: add this line to wp-config.php:
    define( 'CJS_SAFE_MODE', true );
  3. From Settings: turn on Switch off ALL custom code.

Then fix the code, or restore a working version from Revisions.

My code does not appear

  • Check that the field or snippet is Enabled / Active.
  • Check the snippet’s conditions and schedule.
  • Check Do not load site code for administrators – if it is on, log out or open the site in a private window.
  • Clear your cache plugin and your browser cache.
  • Turn on HTML comment markers and search the page source for Custom JS.

Examples

Google Analytics 4 (Head)

<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-XXXXXXX');
</script>

Google Tag Manager (Head + After <body>)

Paste the first part of the GTM code into Add JS in Head and the <noscript> part into Add JS after <body>:

<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>

Track a contact form as a lead (snippet)

Location: Site – Footer. Only on posts/pages: contact. Tick Run after the page has loaded and This code needs jQuery.

jQuery('form').on('submit', function () {
  gtag('event', 'generate_lead', { form: 'contact' });
});

Smooth scrolling for anchor links (Footer)

document.querySelectorAll('a[href^="#"]').forEach(function (link) {
  link.addEventListener('click', function (e) {
    var target = document.querySelector(this.getAttribute('href'));
    if (target) {
      e.preventDefault();
      target.scrollIntoView({ behavior: 'smooth' });
    }
  });
});

Open external links in a new tab (Footer)

document.querySelectorAll('a[href^="http"]').forEach(function (link) {
  if (link.hostname !== location.hostname) {
    link.target = '_blank';
    link.rel = 'noopener';
  }
});

For developers

Custom JS has filters for advanced customisation. Add them to your theme’s functions.php or to a small plugin.

Filter Use
cjs_capability Capability needed to manage the plugin. Default manage_options.
cjs_front_allowed Return false to stop all front-end output on a request.
cjs_snippet_should_run Decide whether a snippet runs. Receives the result, the snippet and the area.
cjs_snippet_code Change a snippet’s code before it is printed.
cjs_output_{option} Change site code before it is printed, for example cjs_output_cjs_head.
cjs_metabox_post_types Post types that show the per-page Custom JS box.

Example: never load custom code on WooCommerce checkout

add_filter( 'cjs_front_allowed', function ( $allowed ) {
    if ( function_exists( 'is_checkout' ) && is_checkout() ) {
        return false;
    }
    return $allowed;
} );

Example: let Editors manage Custom JS

add_filter( 'cjs_capability', function () {
    return 'edit_others_posts';
} );

Security

Every action is protected with WordPress nonces. On multisite networks only users who are allowed to post unfiltered HTML (Super Admins) can save code.

FAQ

Is Custom JS free?

Yes. All features are free.

I am updating from version 1.0. Will my code still work?

Yes. Your Head and Footer code is kept and printed exactly as before. The admin page has the same address, so your bookmarks still work.

Do I need to add <script> tags?

Not when Auto-wrap is on. Code that already contains HTML tags is always printed as it is.

Will my code be lost if I change my theme?

No. The code is stored by the plugin, not in the theme.

Can I add CSS too?

Yes. Choose Raw HTML wrapping and add your CSS inside <style> tags.

Does it slow down my site?

No. The plugin only prints the code you add, and only where your conditions allow it. Use the Footer location or defer for scripts that are not needed right away.

What happens to my code if I delete the plugin?

It is kept, so you can reinstall without losing anything – unless you turn on Delete all data when the plugin is deleted in Settings.

Changelog

2.0.0

  • New: “After <body>” location.
  • New: unlimited snippets with location, priority, on/off switch, duplicate and notes.
  • New: page, URL, user, role, device and schedule conditions.
  • New: external scripts with async, defer and module.
  • New: admin area and login page code.
  • New: per-post and per-page code.
  • New: code editor with highlighting, error checking, dark theme and Ctrl+S.
  • New: revisions, import/export, safe mode and kill switch.
  • New: hide code for administrators or logged-in users.
  • Fully compatible with version 1.0.

1.0

  • Initial release.