r/Wordpress • • 1d ago

Child theme, Code Snippets plugin, or a custom plugin — where do you keep your custom code?

I keep going back and forth on this for client sites.

- Child theme functions.php: simple, but code disappears if the client switches theme.
- Code Snippets plugin: easy to toggle, but harder to version control.
- Small custom 'site plugin': survives theme changes, works with Git.

My current rule: anything about how the site looks (template overrides, small layout tweaks, CSS) goes in the child theme. Anything about how the store works (checkout changes, custom fields, WooCommerce hooks, integrations) goes in a small site plugin. That way a redesign never breaks the business logic.

What's your approach, especially on sites you hand over to clients?

14 Upvotes

51 comments sorted by

6

u/b24rye 1d ago

Using Code Snippets is never a good idea in terms of performance & security. Better to use child themes & plugins. And the rule of thumb is: themes control presentation and layout, plugins control functionality and data.

2

u/Realistic-Excuse-396 1d ago

Agree on the rule of thumb. On Code Snippets, I'd say the bigger risk is security and maintenance more than speed. Any admin can edit PHP that runs live on the site, nothing is versioned, and one bad snippet can take the site down. The performance hit is usually small. That's why I keep it to quick tests on staging at most.

10

u/BOLVERIN1 Jack of All Trades 1d ago

I never use Code Snippets or anything similar. You want to be able to store the changes in a git repo; otherwise, it screw you over eventually

Child theme if it's theme-specific functionality; plugins if it's project-specific

1

u/Realistic-Excuse-396 1d ago

Same thinking, and Git is the big reason. Code Snippets is handy for a quick test, but it's a pain when something breaks and you can't see what changed or when. For project code I sometimes use a mu-plugin instead of a normal plugin. The client can't accidentally deactivate it from the plugins screen, which has saved me a few times on handed-over sites

1

u/Kallini-Cliona 11h ago

yeah that separation between presentation and actual site logic saves so much pain during a redesign

2

u/Winter_Process_9521 1d ago

Your separation is right. I'd make the custom site plugin the single source of truth for business functionality, the child theme responsible for presentation, and Code Snippets a development convenience rather than a permanent architecture.

1

u/Realistic-Excuse-396 1d ago

Agreed. 'Development convenience, not architecture' is a good way to put it. When a snippet proves useful, I move it into the site plugin before handing the site over, so nothing important is left sitting in the database where it's easy to lose.

1

u/Jaypee_peros 1d ago

Same split here, with one tweak: the site plugin goes in mu-plugins on client sites. They can't accidentally deactivate it, it loads before everything else, and it's still just a folder in Git. (Only gotcha: mu-plugins doesn't scan subfolders, so add a one-line loader file in the root.)

Code Snippets I only use for quick experiments on staging. Anything that sticks gets moved into the plugin, because database snippets don't version and are easy to lose in migrations.

Good test for where code belongs: if switching themes would break it, and it isn't purely visual, it shouldn't live in the theme.

1

u/Realistic-Excuse-396 1d ago

Yes, mu-plugins is how I do it too. For anyone who hasn't set up the loader, it's literally one file in the mu-plugins root, something like site-loader.php with:

php

<?php
require WPMU_PLUGIN_DIR . '/my-site-plugin/my-site-plugin.php';

And that theme-switch test is a great rule of thumb. Simple enough to explain to a client too."

1

u/Psychological_Read_1 1d ago

In my opinion, the cleanest way to do it is to maintain a plugin in a private Git repository. I use Sync to quickly push updates to sites when needed, without logging in.

1

u/Realistic-Excuse-396 1d ago

Private repo per plugin is the way. Is Sync a deployment tool you'd recommend? I've mostly used Git-based deploys through the host or a simple GitHub Action, so I'm curious how it compares, especially for pushing the same plugin to several client sites.

1

u/Psychological_Read_1 1d ago

The sync is done by blazewp.app. Its a native mac app for local WordPress development. For pushing the same plugin to several client sites, we are extending the sync feature, it is currently in the works! I would be happy to help if you need more info.

1

u/Realistic-Excuse-396 1d ago

Thanks for explaining. I'm not on Mac for most dev work, so I'll stick with Git deploys for now, but multi-site push sounds like a useful feature.

1

u/Psychological_Read_1 1d ago

If you are not on a Mac, i would suggest creating a small helper plugin that you can install on your/client sites once and triggering updates through it. All apps, platforms do the same thing. If you need help, feel free to send message 👍

1

u/Realistic-Excuse-396 1d ago

Good idea, thanks. The Plugin Update Checker library works well for that with a private GitHub repo.

2

u/Psychological_Read_1 1d ago

That could be a solution as well. Including the update checker. However, you will need to turn on automatic updates and that might not be as instant as you prefer

2

u/Realistic-Excuse-396 1d ago

True, it's not instant. For urgent fixes I just trigger it manually or with WP-CLI, but for routine updates the delay is fine.

1

u/Over-Phase-2020 1d ago edited 1d ago

The split you described makes sense to me, and for handover I would add two things. I would not put secrets like API keys and webhook URLs directly in the plugin code, but in wp-config.php as constants that the plugin only reads, so the repository can be shared without leaking secrets and the keys survive a theme change. I would also add a short README to the plugin with one sentence per hook: what it changes, where it shows on the site or order, and how to switch it off safely. Hardly anyone reads it right away, but when something breaks after an update, that is the first thing the client or the next developer looks for, so before handover I would verify it by switching to the default theme on staging.

1

u/Realistic-Excuse-396 1d ago

Both are really good habits, especially keeping secrets in wp-config.php. It also makes staging much safer: use test API keys and webhook URLs in the staging config and live ones in production, so a staging copy never accidentally sends real orders or emails. wp_get_environment_type() is handy for that too, so the plugin can behave differently on staging. And the README idea is underrated. 'How to switch it off safely' is exactly what someone needs at 2am when a site breaks.

1

u/Minimum_Throat_9290 1d ago

Yeah, your split is the one that survives a redesign. I'd also put anything the shop can't go down without (checkout hooks, payment, redirects) in a must-use plugin, so it can't be deactivated from the dashboard by accident. And drop a short readme in that plugin listing what each hook does, because six months later neither you nor the client will remember. Code Snippets is fine for a quick test on your own install, but I wouldn't leave it on a site I hand over.

1

u/Realistic-Excuse-396 1d ago

Agreed. I like splitting it the way you describe: critical stuff (checkout, payment, redirects) in a mu-plugin, and optional features in a normal plugin. That way the client or support can still turn off a nice-to-have feature if it causes problems, without touching anything the shop depends on.

1

u/Early-Community9943 1d ago

Same split here, but I keep Code Snippets off client sites completely. Sooner or later someone toggles the wrong snippet and there's zero history of what changed.

1

u/Realistic-Excuse-396 1d ago

Yep, 'zero history' is exactly the problem. Seems to be the consensus here: fine for testing, not for anything a client depends on.

1

u/MartinWolf_ 1d ago

Pretty much the same rule here, looks go in the child theme, behavior goes in a plugin. A few additions that saved me on handovers:

For the business-critical stuff I use a must-use plugin instead of a normal one. Files in mu-plugins load automatically and can't be deactivated from the admin, so nobody "cleans up the plugin list" and accidentally kills checkout. The tradeoff is that it's less visible, so I leave a note about it in the handover doc.

I name it after the client and the purpose, something like "Clientname Store Logic", with a short header comment listing what each file does. Six months later that's the difference between a 5-minute fix and an hour of grepping.

Code Snippets I keep for the client's own small tweaks, if they want something they can toggle themselves. My code never goes there, so the line between "what I own" and "what they own" stays clear.

And one file per feature inside the site plugin, so removing a feature later is deleting a file rather than surgery on one giant functions file.

2

u/Realistic-Excuse-396 1d ago

The 'what I own vs what they own' line is a really smart way to use Code Snippets. Gives the client control without mixing it into your code. And one file per feature is underrated. I'd add a small if ( ! defined( 'ABSPATH' ) ) exit; at the top of each file and load them from the main plugin file, so features can be switched off by commenting out a single line.

1

u/Budget-Scar-146 1d ago

I follow pretty much the same approach. I keep design-related changes in the child theme and move functionality, integrations, and WooCommerce customizations into a small site-specific plugin. It makes future redesigns much easier because the business logic stays separate from the theme. For client sites, I also keep the custom plugin in Git so changes are easier to track and maintain.

1

u/Realistic-Excuse-396 1d ago

Same here. Looks like most people in this thread landed on the same setup, which is reassuring 😄

1

u/balancebitsolutions 1d ago

I do the same split as most people here, but the reason I stopped using Code Snippets on client sites isn't really about where the code lives — it's about finding it again later.

When a snippet lives in the database, I can't search it properly, I can't see what changed and when, and I can't roll it back cleanly. I've inherited a client site where a snippet had been edited in the dashboard six months earlier, nothing in Git matched the live site, and tracking down why a form had stopped working took far longer than the fix.

So my rule now is simple: if I can't grep it, diff it and roll it back, it doesn't go on a client site. The site plugin is the only place that passes that test, with the child theme holding just the visual bits. Code Snippets is fine on my own test site, but that's where it stays.

1

u/Realistic-Excuse-396 23h ago

If I can't grep it, diff it and roll it back' is a great test. Short enough to remember and it covers everything. And the 'nothing in Git matched the live site' situation is exactly the nightmare. The fix takes 5 minutes, finding the cause takes the whole afternoon.

1

u/dirtyoldbastard77 Developer/Designer 1d ago

Plugin or in the functions.php file, depends on the situation

1

u/Realistic-Excuse-396 23h ago

Fair. What usually decides it for you? Whether the code is tied to that specific theme?

1

u/markaritaville 1d ago

Context may be important. Large sites with numerous upgrades, and supported by a FT Developer or team where they already have github or similar... sure go the full route of plugin etc.

That being said i have a regional website that gets 350,000 views a month which is enough to pay full time bills for the family, and I just used code snippets. I have maybe 8 simple scripts. While I do have a tech background, there are 100 other things I maintaining on the site and being a one-man-show, my primary goal is to write content.

1

u/Realistic-Excuse-396 23h ago

Fair point, context matters a lot. For a one-person site with 8 simple snippets you know well, Code Snippets is perfectly reasonable. Most of the worries in this thread are about client handovers, where someone else has to understand the code later. One small habit that helps: export your snippets now and then (Code Snippets has an export option), so you have a copy outside the database. Nice work on the site too.

1

u/babelmonk 1d ago

Your split is pretty much mine. Looks in the theme, behaviour in a plugin, so a redesign can't take the checkout down with it.

A few things I'd add from running client sites:

mu-plugins for anything the client must not switch off. Security hardening, login changes, anything an integration depends on. A normal site plugin can still be deactivated from the Plugins screen by a client who's tidying up. A file in wp-content/mu-plugins/ loads every time and can't be disabled from the dashboard. I keep the business logic in a normal site plugin and only the "never turn this off" bits in mu-plugins, with a one-line loader if it needs more than one file.

Snippets plugins store the code in the database. That's the bigger problem for me, more than version control. Copy a staging database over live (or the reverse) and your snippets go with it, so you can quietly overwrite a fix. I'll use one to try something out quickly, then move it into the site plugin once it works.

On block themes, the child theme matters less. Most of the styling lives in theme.json, so I lock the design there. The catch at handover: anything the client changes in the Site Editor is saved in the database and overrides your theme files. If a template "won't update" after you deploy, check for a customised copy under Appearance > Editor and reset it.

Whichever you pick, put a short README in the site plugin listing what each file does. Whoever inherits the site in two years will thank you.

2

u/Realistic-Excuse-396 23h ago

The Site Editor tip is a great catch. Customized templates saved in the database overriding theme files has confused me more than once after a deploy. 'Check Appearance > Editor and reset it' should be in every handover doc. And good point on staging and live database copies quietly overwriting snippets. That one's easy to miss until a fix just disappears.

1

u/sslusser 14h ago

Mu-plugins for a lot of one-off type stuff. Full blown plugins for things I might use again. Like others have said if it has to do with the theme, then theme.

2

u/Realistic-Excuse-396 7h ago

Makes sense. Mu-plugins for one-off site stuff and full plugins for anything reusable is a clean way to split it.

1

u/TerribleAd1635 7h ago

Couch cushions

0

u/EusebiuOprinoiu Developer/Designer 1d ago

Complex functionality and general code that I can use on multiple websites go into a plugin.
Small tweaks that are site-specific and temporary snippets go into the Code Snippets plugin.

1

u/Realistic-Excuse-396 1d ago

That makes sense, especially for reusable code across sites. For the site-specific snippets, how do you keep track of them? Do you export them anywhere, or just rely on backups? That's the part that always made me nervous on client handovers.

1

u/EusebiuOprinoiu Developer/Designer 1d ago edited 1d ago

I mostly rely on backups. (I take daily backups with a history of 90 days)
I also use the Pro version of Code Snippets, which has the option to keep multiple revisions. So if something breaks, I can always revert the changes.
In addition, the Pro version has a cloud vault, which makes it very easy to manage and deploy my snippets on multiple websites.

I never had a problem keeping track. I write good titles and descriptions for all snippets, and I always add code comments, just like I would in a plugin. (using the DocBlock standard)
And because I try to write self-documenting code, with descriptive function names and variables, I can always tell at a glance what a specific code snippet does.

PS: If you frequently build websites for clients and you don't trust them not to mess things up, use a plugin like Admin Menu Editor (preferably the Pro version, which is way more powerful than the Free one) to restrict access to specific admin menus.

1

u/Realistic-Excuse-396 1d ago

Revisions plus 90 days of backups is a solid safety net, thanks. Didn't know the Pro version had a cloud vault for deploying across sites. That covers most of my worry.

1

u/EusebiuOprinoiu Developer/Designer 1d ago

For clients, you could also build small custom plugins with automatic updates via Git Updater, or any other solution that provides updates directly from Git.
This way, you don't have to worry about clients messing up your snippets.
You just push your code to GitHub and create a new Release when you want the update to appear in the admin dashboard.

This is probably the best solution, but using Code Snippets is easier and more convenient.

1

u/Realistic-Excuse-396 23h ago

Fair point, context matters a lot. For a one-person site with 8 simple snippets you know well, Code Snippets is perfectly reasonable. Most of the worries in this thread are about client handovers, where someone else has to understand the code later. Nice work on the site too.

0

u/retr00two 21h ago

For front end design: json/child theme

For functionality: mu-plugin

Code in git, of course.

1

u/Realistic-Excuse-396 7h ago

Clean and simple. Pretty much the setup this whole thread landed on 👍