Code injection lets you add snippets of code to your HTML pages without editing the files. There are two levels, and you can use both together.
Why you need it:
Your HTML pages are served on their own, without your theme. That keeps your design clean, but it also means anything your theme or other plugins normally add to your site, such as analytics tags, pixels, chat widgets or cookie notices, is not added to these pages. Code injection is how you add the ones you want.
Site-wide code. Every page:
- Go to HTML Pages, Settings and scroll to Code Injection.
- Paste your snippet into Head Code, which is injected before </head>, or Body Code, which is injected before </body>. Most analytics and pixel instructions tell you which one to use. If they say paste it in the head, use Head Code.
- Click Save Settings.
Per-page code. One page only:
- Open the page from All HTML Pages.
- Under the upload box, find Page Code Injection (this page only).
- Paste your code into Head Code or Body Code.
- Click Update. The code is saved even if you do not upload a new file.
What order the code goes in:
On each page, the site-wide code goes in first and the page's own code follows it.
Good uses:
- Analytics tags on every page (site-wide)
- Advertising or retargeting pixels (site-wide)
- A conversion pixel on one thank-you page (per page)
- A chat widget (site-wide or per page)
- Extra CSS inside style tags to tweak one page
- Meta tags, such as a site verification tag or a noindex tag
Rules and gotchas:
- Paste the complete snippet, including its script or style tags. Your code is saved exactly as you type it and is not checked, so a broken snippet can break the page. Only paste code from sources you trust.
- Your HTML file needs its closing tags. If a file has no </head>, the head code is not added to it. If it has no </body>, the body code is not added. Upper or lower case tags both work.
- If your file contains the text </head> or </body> more than once, for example inside a script, the code is added at every one of them. Keep one of each.
- Injection only applies to pages served by this plugin. Your normal WordPress posts and pages are not touched.
- Duplicate copies a page's own code along with the page. Change the code in the copy if it should differ.
- On a page uploaded as a ZIP, the site-wide code and the page's own code go into every .html page in the ZIP.
- To remove code, empty the box and save.
Check it worked:
Open your page, right-click and choose View Page Source. Your code should be just above </head> or </body>. The Diagnostics tab also shows whether site-wide head and body code are set.