On this page
Code injection lets you add your own code to a Ghost (opens in a new tab) 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 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}} (opens in a new tab) helpers, which every Ghost theme has in its default.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.
Styles go in the site header, inside <style> tags:
<style>
/* Your CSS */
</style>Scripts usually go in the site footer, inside <script> tags:
<script>
/* Your JavaScript */
</script>For one post or page: open it in the editor, then open Code injection in the 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:
<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:
<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.
Small features
Scripts can add features your theme doesn’t have. For example, this script opens links to other sites in a new tab:
<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.
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. 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 is usually the better fit.