Found a PHP code snippet online that does exactly what you need for your WordPress site — but not sure where to put it safely? You’re not alone. This is one of the most common questions we get from store owners and developers alike.
The short answer: never paste code directly into a theme or plugin file. There are four proper ways to add PHP snippets to WordPress, and this guide covers all of them — from the simplest (a plugin) to the most powerful (a must-use plugin).
A Word of Warning Before You Start
Copying and pasting PHP code from the internet into your WordPress site carries real risks. Before you add any snippet, keep these in mind:
- Know what the code does. If you’re not familiar with PHP and WordPress security best practices, only use snippets from sources you absolutely trust.
- Check the code in a proper text editor — one with syntax highlighting, like VS Code, Atom, or Notepad++. Look for correct single quotes (
'), proper semicolons (;) at the end of each statement, and no corrupted characters. - Always take a full backup first. If something goes wrong, you need to be able to restore your site. Make sure you also have FTP or SSH access so you can fix issues even if the WordPress admin becomes inaccessible.
Where NOT to Add PHP Code
Before we cover the right ways, let’s address the most common wrong way.
Do not edit a theme or plugin file directly.
Up until a few years ago, “add this to your theme’s functions.php” was everywhere. The problem: every time you update your theme or plugin, any changes you made to those files are completely wiped out. And you should never, under any circumstances, edit WordPress core files.
The correct alternatives are below.
Method 1: Use the Code Snippets Plugin (Easiest — Recommended for Most Users)
The Code Snippets plugin is the safest and most user-friendly way to add PHP to WordPress. It gives you a dedicated interface to add, manage, enable, and disable snippets — completely separate from your theme and plugin files.
How to use it:
- Go to Plugins > Add New and search for “Code Snippets”
- Install and activate the plugin
- Go to Snippets > Add New in your dashboard
- Give your snippet a title, paste your PHP code in the code box
- Choose when to run it (front-end, admin, or everywhere)
- Click Save Changes and Activate

Why this is the best method for most users:
- Snippets survive theme and plugin updates
- You can toggle snippets on/off without deleting them
- If a snippet causes a fatal error, the plugin has a built-in safety net
- No file editing or FTP access required
Method 2: Add Code to a Child Theme’s functions.php
If you don’t want to install another plugin, the next safest option is a child theme. A child theme sits on top of your parent theme and holds your customizations — when the parent theme updates, your child theme (and your code) remains untouched.
Step 1: Create a child theme
The easiest way is using the free Child Theme Generator plugin:
- Install and activate the plugin
- Go to Settings > Child-Theme Gen
- Select your parent theme from the dropdown
- Fill in the optional fields (theme name, author, etc.)
- Click Create Child Theme

Step 2: Add your code
Once your child theme is active, go to:
Appearance > Theme Editor > Theme Functions (functions.php)
Make sure you’re editing the child theme’s functions.php (it will say your child theme name at the top). Scroll to the bottom and paste your snippet there.
When to use this method: Best for snippets that are tightly tied to your current theme’s functionality — like adding custom hooks, modifying theme layout behavior, or extending theme-specific features.
Method 3: Create a Custom Plugin
Creating a simple custom plugin sounds more advanced than it is. The benefit: your snippet works independently of any theme even if you switch themes, the code keeps running.
Step 1: Access your site via FTP, cPanel, or Plesk File Manager and navigate to:
/wp-content/plugins/
Step 2: Create a new folder called my-custom-plugin
Step 3: Inside that folder, create a file called my-custom-plugin.php

Step 4: Open my-custom-plugin.php in a text editor and add the following header, then paste your snippet below it:
<?php
/**
* Plugin Name: My Custom Plugin
* Plugin URI: https://yourdomain.com
* Description: Custom functionality for yourdomain.com
* Version: 1.0
* Author: Your Name
* Author URI: https://yourdomain.com
*/
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
die;
}
// Your code here...
Step 5: Go to Plugins > Installed Plugins in your WordPress dashboard and activate your new plugin.
When to use this method: Best when you want your snippet to work regardless of which theme is active, or when you’re building functionality that logically belongs in a plugin rather than a theme.
Method 4: Create a Must-Use Plugin (MU Plugin)
Must-Use Plugins (MU Plugins) are a special type of WordPress plugin that:
- Load automatically — they don’t need to be activated in the Plugins screen
- Cannot be deactivated through the WordPress admin
- Load before any other plugin — making them ideal for critical functionality
How to create one:
Step 1: Navigate via FTP or File Manager to:
/wp-content/
Step 2: If a folder named mu-plugins exists, open it. If not, create it.
Step 3: Create a PHP file inside it — e.g. my-custom-muplugin.php with the same plugin header format as Method 3.
Step 4: Paste your snippet below the header. Done, no activation needed.
When to use this method: Best for critical, site-wide functionality that must always run like security rules, custom redirects, or functionality that must load before other plugins.
Quick Comparison: Which Method Should You Use?
| Method | Survives Theme Update | Survives Plugin Update | Requires FTP | Difficulty |
|---|---|---|---|---|
| Code Snippets Plugin | ✅ | ✅ | ❌ | Easy |
| Child Theme functions.php | ✅ | ✅ | ❌ | Easy |
| Custom Plugin | ✅ | ✅ | ✅ | Medium |
| MU Plugin | ✅ | ✅ | ✅ | Medium |
Wrapping Up
There’s no shortage of useful PHP snippets for WordPress and WooCommerce but where you put them matters as much as what they do. Here’s the quick recap:
- Code Snippets plugin → best for most users, safest, no file editing needed
- Child theme functions.php → good for theme-specific tweaks, survives updates
- Custom plugin → theme-independent, easy to disable if something breaks
- MU plugin → loads first, always active, ideal for critical functionality
Always back up before adding any code, test on staging when possible, and only use snippets from sources you trust.
