# WordPress snippets: where to put AI-written code safely

URL: https://ekom.blog/wordpress-snippets-where-to-put-ai-written-code-safely/
Published: 2023-10-04
Author: Ekom
Topics: Build with AI

> Where AI-written code should live in WordPress: snippets plugin, child theme or custom plugin, and why the functions.php file is the riskiest option.

AI is very happy to hand you a block of PHP and say "add this to your functions.php". It is much less helpful about the question that matters more: where should that code actually live?

Put it in the wrong place and a typo can lock you out of your own site. Put it in the right place and the same typo becomes a small, reversible problem. Here is how I think about it.

## The short answer

For most people: use a snippets plugin for small pieces of code, a small custom plugin for anything you will keep, and a child theme only for things that are really about appearance. Avoid editing your main theme's functions.php directly, and never edit it through the dashboard editor on a live site.

## Why the location matters

PHP is unforgiving. A missing semicolon in a file that WordPress loads on every request can produce the white screen of death. If that file is your theme's functions.php, you may not even reach the dashboard to undo the change.

Location decides three things:

- How you recover when it breaks.

- Whether the code survives a theme change or update.

- Whether you can find it again in a year.

## Option 1: a snippets plugin

A snippets plugin stores small bits of PHP, CSS or JavaScript in its own interface, runs them for you and lets you switch each one on and off.

Good for: short, self-contained changes, such as adding a function that changes a text string, hiding a field or tweaking an excerpt length.

Why it is safer: most snippet plugins check the code for errors before activating it and can deactivate a snippet that breaks the site. Each snippet is separate, so you can turn off the culprit without touching anything else. And the code survives theme changes.

The catch: you now depend on one more plugin. Keep it updated, and use one that is actively maintained. Also, snippet lists can grow into a junkyard, so name them clearly and add a note about what each does.

## Option 2: a small custom plugin

If a piece of code does something that your site needs permanently, such as registering a custom post type or adding a shortcode, it belongs in a plugin. A plugin can be one PHP file with a short header comment, uploaded to wp-content/plugins/.

Good for: site-specific features you want to keep regardless of the theme.

Why it is better than the theme: your content structure should not vanish when you switch themes. A custom post type that lives in the theme disappears with it. That is the reason many developers argue that functionality belongs in plugins and design belongs in themes. If you are curious about how AI can help with this, custom post types with AI help goes through it.

The catch: a bit more setup, and you must keep it tidy.

## Option 3: a child theme

A child theme sits on top of your main theme and holds your changes, so the parent can update without wiping them. It is the right home for theme-related changes: template overrides, styling and display tweaks. We looked at that in theme tweaks with AI: child themes and safe edits.

The catch: a child theme's functions.php has the same risk as any other. A syntax error there will still break the site. It protects you from updates, not from typos.

## Option 4: the theme editor on a live site (don't)

The built-in theme file editor is tempting. It is also how many people take their sites down in one click, with no version history and no easy way back. If your host allows it, consider disabling the file editor entirely. If you must edit a file, do it with SFTP and a copy of the original, or on a staging site.

## A safe routine for any AI-written code

Whatever location you pick, this routine keeps the risk low.

- Back up first. A fresh backup, or at least a copy of the file.

- Ask the AI to explain the code line by line. How to ask AI to explain code line by line shows a good prompt.

- Test on staging, or a local copy, if you can.

- Add it in a snippets plugin or a plugin file, not the theme editor.

- Check the site front end, dashboard and the page where the change should show.

- Write a one-line note saying what it does and when you added it.

- Know the undo. Every snippet should have an obvious way to switch it off.

## Things to read for before activating

- Anything that deletes, updates or queries the database.

- Anything that sends data to another website.

- Unfamiliar function names, since models sometimes invent them.

- Hard-coded passwords or keys. They should not be there.

- Code that modifies user roles or permissions.

If anything in that list is unclear, do not add the code until you understand it. For more on this, copying code from AI safely: what to check first covers the broader checklist.

## How I manage several sites

When you look after more than one site, the biggest risk is forgetting what you added where. I keep a short note per site listing every snippet or custom plugin, what it does and when it went in. It is the same habit as keeping one inventory and one change log per site. A snippet you cannot explain six months later is a liability.

## Key takeaways

- Small, temporary changes go in a snippets plugin.

- Permanent site features go in a small custom plugin.

- Appearance changes go in a child theme.

- Do not edit live theme files in the dashboard editor.

- Back up, understand the code, test and keep a note.

## Related reading

- Copying code from AI safely: what to check first

- Theme tweaks with AI: child themes and safe edits

- Automating boring WordPress tasks with AI-written snippets

Where is the oldest piece of unexplained code on your site, and do you know what it does?

---

From Ekom (https://ekom.blog/). More: https://ekom.blog/llms.txt
