Skip to content
WordPress Guide

Code Snippets Plugin vs functions.php: Which to Use

Code pasted into a theme's functions.php disappears on the next theme update and can take the site down. Here is a safer home for small changes, and how to judge a snippet before you use it.

A woman working on a tablet with a keyboard at a cafe table
8 sections · 7 min read
In this article · 8 sections

A tutorial has the answer to your problem, and it ends with a box of code and the line “add this to your theme’s functions.php file”. Perhaps it removes a label from your shop, changes the length of excerpts, or turns off a feature you do not want. The code looks short. The instruction sounds simple.

Before you paste anything, it helps to know where that code will live and what happens to it later. This post compares the Code Snippets plugin vs functions.php for a site owner adding small pieces of PHP, explains why the theme file is usually the wrong home, and shows how to add code to WordPress in a way that survives updates and can be switched off in one click. It is one of the plugin topics in our WordPress help guide for owners.

What functions.php is, and why tutorials point there

Every theme can have a file called functions.php. WordPress loads it on every page, front end and dashboard alike, so any PHP code placed in it runs everywhere. That makes it the most convenient place for a tutorial writer to send you: it exists on almost every site and needs no extra plugin.

The convenience is for the writer, though. For you, the file has three real drawbacks.

Three problems with editing the theme’s file

A theme update wipes it

When your theme updates, WordPress replaces the theme’s files with the new version. Your added lines are gone, and nothing warns you. The feature you changed quietly goes back to how it was, often weeks later, when nobody remembers why.

Changing theme drops it

Code in functions.php belongs to that theme. Switch to a new theme during a redesign and every customisation in the old file stops running. Some of those may have been doing important work, such as adjusting checkout fields or blocking a form of spam.

One mistake can take the site down

A missing semicolon or bracket in functions.php is a fatal error on every page, often shown as “There has been a critical error on this website”. The built-in file editor in recent WordPress versions tries to catch such errors and undo the save, and WordPress can email the site admin a recovery mode link, but neither is a promise. If you are locked out, the fix means opening the file by FTP or the hosting file manager. Our page on the WordPress critical error explains that situation.

For reference, the built-in editor is at Tools → Theme File Editor in block themes and Appearance → Theme File Editor in classic themes. Some hosts and security plugins hide it on purpose, so do not worry if it is missing.

Code Snippets plugin vs functions.php, side by side

A snippets plugin stores each piece of code separately in the database and runs it for you, independent of the theme.

Theme functions.php Code Snippets plugin
Survives a theme update No, unless it is a child theme Yes
Survives a theme change No Yes
Turn one change off Edit the file and delete lines A toggle in a list
Labelled and described Only if someone added comments Each snippet has a title and description
Help if the code has an error Limited Checks code on save, plus a safe mode

For most owners adding a few small changes, the plugin is the better choice. A similar plugin, WPCode, works along the same lines; pick one and stick with it rather than spreading snippets across both.

Adding a snippet with Code Snippets

  1. Take a backup, or at least make sure your host keeps a recent one.
  2. Go to Plugins → Add Plugin, search for Code Snippets, then install and activate it. A new Snippets menu appears in the dashboard.
  3. Open Snippets and add a new snippet.
  4. Give it a plain title that says what it does, such as “Hide related products on shop pages”.
  5. Paste the code into the code box. Leave out the opening <?php line if the tutorial includes one; the plugin supplies that for you.
  6. Choose where it should run. The plugin lets you limit a snippet to the dashboard or the front end instead of everywhere, which is safer when the code only concerns one side.
  7. Write a short description: where the code came from and why you added it. That note is what saves the next person from guessing.
  8. Save and activate, then check the page the snippet affects and a couple of other pages.

If a snippet breaks the site anyway

Code Snippets tries to check a snippet for errors when you save it and will not activate code it can see will fail. Some problems only show up when the code runs on a particular page, so that check cannot catch everything. If the site breaks after you activate a snippet:

  • If you can still reach the dashboard, open Snippets and switch the new snippet off.
  • If you cannot, the plugin offers a safe mode that stops all snippets running so you can log in and switch off the faulty one. Its documentation explains how to turn it on; at the time of writing that meant adding define( ‘CODE_SNIPPETS_SAFE_MODE’, true ); to wp-config.php through your hosting file manager, then removing the line once you are back in. Check the current documentation before relying on it.

Other sensible homes for code

  • A child theme’s functions.php. If the code is about how your theme looks or lays things out, and you already use a child theme, it can live there. A child theme is not changed by parent theme updates. Our post on whether you need a WordPress child theme explains when one is worth having.
  • A small custom plugin. When the code is large, important or growing, such as business rules for your shop, it belongs in a proper plugin, written and maintained like any other. That is the kind of work our custom plugin development page covers.
  • Not PHP at all. If the tutorial gives you CSS rather than PHP, it does not go in functions.php or a PHP snippet. Use the Additional CSS area instead, explained in our post on how to add custom CSS in WordPress.

Where snippets come from, and how to judge one

Code is only as good as its source. Snippets come from developer blogs, plugin documentation, forum answers, AI chatbots and developers you have hired. Before pasting one, ask:

  • Who wrote it, and when? Code from a plugin’s own documentation or a well-maintained blog is a better bet than an anonymous comment. Old snippets may use functions WordPress has since retired.
  • Do you understand roughly what it does? You do not need to read PHP, but the explanation around it should match your goal. If it claims to do one thing and is much longer than you expected, be suspicious.
  • Does it do anything alarming? Code that creates user accounts, contacts an outside address, or contains long strings of scrambled characters has no place in a simple tweak. Do not run it.
  • Has it been tested on a copy? Especially for code from a chatbot, which can look confident and still be wrong. Try it on a staging site first if you can.

Keep one snippet per job, so you can switch off one change without losing the others.

Keeping your snippets tidy

Every snippet is a small piece of custom software on your site. List them in the same record you keep for plugins, with what each does and where it came from. Code Snippets can export your snippets to a file, which is worth doing before big changes. Deactivate and delete snippets you no longer need, rather than leaving a long list of half-remembered fixes.

The guide to WordPress plugins explains how snippets sit alongside the plugins you run.

Frequently asked questions

I already added code to functions.php. Should I move it?

If it is in a parent theme, yes, before the next theme update removes it. Copy the code into a new snippet, but do not activate it yet. Remove the code from functions.php and save, then activate the snippet straight away. Running both copies at once can cause a fatal error, because PHP refuses to define the same function twice.

Can I copy my snippets to another website?

Usually, yes. Code Snippets can export snippets to a file that another site running the same plugin can import. Before switching them on there, read each one again: a snippet may refer to a page, product or plugin that exists only on the original site. Import them switched off, then activate and test one at a time.

Is it safe to use code written by an AI chatbot?

Treat it like code from a stranger on a forum. It can be correct, and it can also call functions that do not exist or quietly do more than you asked. Ask the chatbot to explain each part, test the snippet on a staging copy first, and keep it switched off on the live site until it works there.

What happens to my snippets if I deactivate Code Snippets?

They stop running, because the plugin is what runs them, but they stay stored in the database. Reactivate the plugin and your snippets are there again with their on and off settings. If you plan to remove the plugin for good, move any snippets you still need elsewhere first.

If a snippet does take the site down and you cannot get back in, that is the kind of problem our WordPress bug fixing service exists for, whether the code went into a snippets plugin or straight into the theme.

Next in this series: Plugin Not Tested With Your Version of WordPress?

Is Your Website Working Properly?

Get a free website health check from WebMaintor. We'll review your website's speed, security, forms, and technical health — and tell you exactly what needs attention.

No obligation. No automated reports. A real review by our team.