# How code injection works in Ghost

Explainer · Norbert · Updated 12 Nov 2026 · 3 min read

Source: https://brightthemes.com/blog/ghost-code-injection

Code injection adds your own styles and scripts to a Ghost site, on every page or on one post, without editing the theme. Where it goes, how it works and what to use it for.

Code injection lets you add your own code to a [Ghost](https://brightthemes.com/ghost/pro) site straight from Ghost Admin: CSS to adjust the design, scripts for third-party tools, or small features your theme doesn't have. It goes further than the [custom theme settings](https://brightthemes.com/blog/ghost-custom-theme-settings) a theme offers, and it keeps your code separate from the theme, so it stays in place when you update the theme.

## How it works

Code injection has two boxes, for two places in each page:

- **Site header:** added inside `<head>`. Use it for styles and for code that must load early, such as meta tags.
- **Site footer:** added before the closing `</body>` tag. Use it for scripts that run after the page loads, such as widgets and tracking.

Your theme outputs them with the [`{{ghost_head}}` and `{{ghost_foot}}`](https://docs.ghost.org/themes/helpers/utility/ghost_head_foot) helpers, which every Ghost theme has in its `default.hbs`:

```hbs
<html>
  <head>
    <title>{{meta_title}}</title>

    {{ghost_head}} {{!-- Site header code goes here --}}
  </head>

  <body class="{{body_class}}">
    {{!-- Your elements and content --}}

    {{ghost_foot}} {{!-- Site footer code goes here --}}
  </body>
</html>
```

Most themes put `{{ghost_head}}` after their own stylesheet, so your CSS loads last and wins over the theme's when both set the same thing with the same selector.

Code injection works at two levels:

- **Site-wide:** on every page of the site.
- **Per post or page:** only on that post or page, on top of the site-wide code.

## Where to find it

**For the whole site:** go to **Settings › Advanced › Code injection** and choose **Open**. The editor has two tabs, **Site header** and **Site footer**. Choose **Save** when you're done.

*A Ghost Admin screen, described in the steps.*

Styles go in the site header, inside `<style>` tags:

*Site header:*
```html showLineNumbers
<style>
  /* Your CSS */
</style>
```

Scripts usually go in the site footer, inside `<script>` tags:

*Site footer:*
```html showLineNumbers
<script>
  /* Your JavaScript */
</script>
```

**For one post or page:** open it in the editor, then open **Code injection** in the [post settings](https://brightthemes.com/blog/ghost-post-settings) sidebar. It has a **Post header** and a **Post footer** field (**Page header** and **Page footer** on a page), which work the same way.

## What to use it for

### Custom styles

Themes give you a design to start from, and sometimes you want to change a detail: a color, a font size, spacing. Add the CSS in the site header:

*Site header:*
```html showLineNumbers
<style>
  .header { background-color: #1e3a8a; }
  .post-hero-title { font-size: 4rem; }
</style>
```

The class names depend on your theme. To find them, inspect the element in your browser: in most browsers, open its context menu and choose **Inspect**.

Per-post code injection suits styles for one post only, such as a landing page without the site header.

### Third-party tools

Most tools that run on your site give you a snippet to paste: analytics, chat widgets, ad networks, cookie banners. Paste it where the tool says, usually the site footer. Google Analytics, for example:

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

Replace `G-XXXXXXXXXX` with your measurement ID.

> [!TIP]
> Since Ghost 6, Ghost has its own web analytics, in **Analytics** in Ghost Admin, with no code to add: on Ghost(Pro)'s Publisher plan and higher, and available on self-hosted sites that run Ghost's Docker setup. You only need a script like this if you want another tool as well. See [how to track your site's traffic in Ghost](https://brightthemes.com/blog/ghost-analytics).

### Small features

Scripts can add features your theme doesn't have. For example, this script opens links to other sites in a new tab:

*Site footer:*
```html showLineNumbers
<script>
  document.querySelectorAll('.gh-content a[href^="http"]').forEach((link) => {
    if (link.hostname !== window.location.hostname) {
      link.target = '_blank';
      link.rel = 'noopener';
    }
  });
</script>
```

It checks each link in the post content, and when the link's domain isn't yours, it adds `target="_blank"`. For a version that also handles `www` and more on whether to do it, read [open external links in a new tab](https://brightthemes.com/blog/ghost-external-links).

## When to use it, and when not

Code injection is right for small changes: a few CSS rules, a tool's snippet, a short script. It is quick, needs no theme upload, and survives theme updates.

For bigger changes, edit the theme instead, on your computer or with [**Edit code** in Ghost Admin](https://brightthemes.com/blog/ghost-theme-code-editor). Large amounts of CSS or JavaScript in code injection are hard to maintain, are added to every page, and can slow your site down. For code in one spot of a post, the editor's [HTML card](https://brightthemes.com/blog/ghost-editor-cards) is usually the better fit.
