WordPress websites and business systems

How to Add Code Snippets to WordPress Safely

Where to put a code snippet in WordPress so it won’t crash the site or vanish after an update: a snippets plugin or a child theme.

Chris Eggleston
Chris Eggleston WP Mantis
Updated November 14, 2026 /  
Code snippets. Adding custom code without breaking the site.

Tutorials (including ours) often say “add this snippet to your site.” Where you put it matters. The wrong place can crash the site, or lose your code the next time the theme updates. Here are the two safe options and the ones to avoid.

First, back up

Take a backup, or work on a staging copy if your host provides one. A single missing semicolon in PHP can take a site down.

Option 1: A snippets plugin (recommended)

A snippets plugin stores each piece of code separately, lets you turn snippets on and off, and catches many errors before they break your site. We use WPCodeBox on client sites; the free Code Snippets plugin works the same way for most needs.

  1. Install and activate the plugin.
  2. Create a new snippet and give it a clear name, like “Use GD for image uploads.”
  3. Choose the code type (PHP, CSS or JavaScript) and where it should run.
  4. Paste in the code and save.
  5. Turn the snippet on, then check the site in a private browser window.

Snippets survive theme changes and updates, and anyone looking after the site later can see exactly what was added and why.

Option 2: Your child theme’s functions.php

If you already use a child theme, its functions.php is a fine home for code that belongs to the site’s design. Edit it over SFTP or your host’s file manager, not from the dashboard, so you can undo a mistake immediately. Add your code at the end of the file, and don’t add a closing ?> tag.

Never add code to a parent theme’s functions.php. The next theme update replaces the file and your code disappears.

What to avoid

  • The dashboard’s theme file editor. One typo and you’re locked out with no way to undo it from the dashboard. We turn this editor off on client sites with DISALLOW_FILE_EDIT.
  • Editing plugin files. Updates overwrite them, and you lose the change.
  • Copying code you don’t understand from old tutorials. Check the date, and test on staging first.

If a snippet breaks the site

WordPress has a built-in recovery mode. When a fatal error happens, it emails the site’s admin address a special link that logs you in with the faulty plugin or theme paused. In a snippets plugin, turn the bad snippet off. If you edited functions.php, remove the code over SFTP. Our guide to handling a WordPress critical error covers this in detail.

Want someone to add it?

If you’d rather not touch code on a live site, our $79 one-time fix can add and test it for you. On a Care Plan, small changes like this are covered.

Chris Eggleston
Chris Eggleston
Husband. Father to 4. Grandpa to 2. Gravity Forms enthusiast. As the owner of WP Mantis, I’m on a mission to simplify the WordPress experience for site owners. I also try to bring a unique perspective to the Gravity Forms community at gravityranger.com.
Next step Not sure what your site needs next?

We will look at it properly and tell you the five things worth doing, in order, whether or not you hire us.

Affiliate links disclaimer