Adding custom PHP code snippets to your website
Many GravityKit docs include a PHP code snippet that changes how a product works. For CSS or JavaScript, see Adding custom CSS to your Views and Adding custom JavaScript to your Views.
Choosing where to add the code #
| Where | When to use it |
|---|---|
| A code snippets plugin, such as Code Snippets | Use it for most snippets. You paste the code in the WordPress dashboard and can switch each snippet off without editing a file. |
| A must-use plugin | Use it for a snippet that has to run while plugins load, or one that has to keep running whichever plugins and theme are active. WordPress loads it before every other plugin. |
| Your theme’s functions.php | Use it only in a child theme, since a theme update replaces the file. WordPress loads it after every plugin. |
Using the Code Snippets plugin #
- Go to Plugins, then Add Plugin, and search for
code snippets. - Click Install Now on Code Snippets, then Activate.

- Go to Snippets, then Add New.
- Enter a title you’ll recognize later, such as the doc the snippet came from, and paste the code under Snippet Content. If the code starts with
<?php, Code Snippets removes that tag when it saves. - Leave Location set to Run everywhere and click Save and Activate.

To turn a snippet off later, go to Snippets, then All Snippets, and click its switch.
Using a must-use plugin #
WordPress loads must-use plugins before your other plugins, and loads a theme’s functions.php after all of them. A snippet that a plugin reads while it loads works from a must-use plugin and has no effect from functions.php.
- Open your site’s files with SFTP or your host’s file manager.
- Go to the
wp-contentfolder, and create a folder namedmu-pluginsif there isn’t one. - In
mu-plugins, create a file whose name ends in.php, such asgravitykit-snippets.php. - Paste this at the top of the file, add your snippets below it, and save the file:
<?php
/**
* Plugin Name: GravityKit snippets
* Description: Custom code from the GravityKit docs.
*/
// Paste your snippets below this line.The code runs on every page as soon as the file is saved. WordPress loads only the .php files directly inside mu-plugins, so a file in a subfolder doesn’t run.
To check that the file loaded, go to Plugins and click the Must-Use tab. The plugin is listed there with the name from its header. It has no Deactivate link, so you turn it off by deleting the file.

Warning: A PHP error in a must-use plugin can stop your whole site. WordPress can pause a broken plugin or theme, but not a must-use plugin, so keep SFTP or your file manager open to fix or delete the file.
Using your theme’s functions.php #
Add code to functions.php only in a child theme. A theme update replaces the parent theme’s functions.php, along with anything you added to it. Child themes in the WordPress handbook shows how to make one.
- With a block theme, go to Tools, then Theme File Editor. With a classic theme, go to Appearance, then Theme File Editor.
- Select your child theme under Select theme to edit, click Select, then click functions.php under Theme Files.
- Paste the snippet at the end of the file and click Update File.
Until you click I understand, the editor opens with a Heads up! message that warns against editing a theme directly and suggests a child theme. If your host turns the editor off, the menu item isn’t there, and you edit functions.php over SFTP instead.
Pasting code into a file #
When you add a snippet to the end of a PHP file, leave out the snippet’s own opening <?php tag. If the file ends with ?>, paste the snippet above that line.
An opening <?php pasted while PHP is still open is a syntax error, and WordPress shows a critical error instead of the site until you remove it. This is what that looks like:
<?php
// The code already in the file.
$existing_code = 'example';
<?php // <--- This tag was pasted with the snippet.
$new_code = 'new code you pasted';Without the second <?php, the file is correct:
<?php
// The code already in the file.
$existing_code = 'example';
$new_code = 'new code you pasted';