Site settings

Custom code and snippets

Kufi → Custom code holds code snippets (PHP, HTML, CSS and JavaScript that run where and when you choose) and site-wide code for the head, the start of the body and before the closing body tag, such as analytics, tag managers and chat widgets.

Go to Kufi → Custom code. Only administrators can see it. It has three tabs: Snippets, Library and Site-wide code.

Snippets

A snippet is a piece of code with its own place and conditions, so you never edit theme files. Click Add snippet.

Type What you write Where it can run
PHP PHP without the opening <?php tag Everywhere, the site only (not in WordPress admin), WordPress admin only, or where its shortcode is placed
HTML Any HTML In the head, at the start of the body, before the closing body tag, before or after the page content (builder pages too), or by shortcode
CSS Plain CSS; the <style> tag is added for you In the head or before the closing body tag
JavaScript Plain JavaScript; the <script> tag is added for you In the head or before the closing body tag

Snippets placed by shortcode show their shortcode on the edit screen, for example . Put it in a post or a page written in the WordPress editor.

Priority orders snippets in the same place: lower numbers run first. Switched on turns a snippet on or off; the list has an On/Off switch too.

Conditions

  • Visitors: everyone, signed-in users, or visitors who are not signed in.
  • Pages: all pages, the home page only, only some pages or posts (by ID), or only some content types.
  • Never on these IDs, Languages (with a multilingual plugin) and Starts / Ends dates (in the site's time zone) are part of Builder Pro. Without Pro they are greyed out; settings saved earlier keep working.

When a snippet breaks something

  • PHP is checked when you save. A snippet with a syntax error is saved but left off, with the error and its line.
  • A snippet that throws an error, or stops a page with a fatal error, is switched off by itself. The list shows the error, and administrators see a notice.
  • Safe mode stops all snippets for you only, while visitors still get them: use the link on the Custom code page, fix or switch off the snippet, then click Leave safe mode.
  • If WordPress admin can't load at all, add define( 'KUFI_SAFE_MODE', true ); to wp-config.php, fix the snippet, then remove the line.

Earlier versions

With Builder Pro, every save keeps the previous version. Earlier versions on a snippet opens WordPress's revisions screen, where you compare and restore.

Export and import

Export (one snippet) or Export all downloads a JSON file. Import on another site adds them switched off, so you can check them first.

Who can do what

PHP snippets are for administrators only. Editors can't see PHP snippets' code, export or restore them, and can't add PHP.

Library

Ready-made snippets: Google Analytics 4, Google Tag Manager, Meta Pixel, turn off comments everywhere, admin bar for administrators only, excerpt length, keep 10 revisions, open external links in a new tab, and smooth scrolling. Use this snippet adds it switched off, so you can fill in your IDs before you turn it on.

Site-wide code

Four boxes for code that applies to every page:

Box Use it for
CSS for every page Styles shared by several pages. Loaded on every page and in the builder. For one element, use its Advanced tab instead.
Code in the head Meta tags, analytics loaders.
Code at the start of the body Tools that ask for code right after <body> opens, such as a tag manager's noscript part.
Code before the closing body tag Page behavior and chat widgets. Runs after Kufi's own scripts.

Click Save custom code. The last 10 saves are listed under Earlier versions; Restore puts one back, and the code it replaces stays in the list.

Scripts and the Content Security Policy

<script> and <style> tags from snippets and site-wide code get the page's security nonce automatically, so they run. Services that then load more files from their own servers (analytics, chat, pixels) need those domains allowed for visitors. See Something is blocked by the Content Security Policy, which includes the lines for Google Analytics.

Where else code can go

  • One spot on one page: the Custom code element.
  • Files such as fonts, or template changes: your child theme. Never edit the kufi theme folder, because updates replace it.

More in Site settings

Still need help?

Tell us what you are trying to do and what you see. We read every message.