Setup & site building · Guide
How to Add Code Snippets to WordPress Safely

Short answer
Add code snippets safely in WordPress with the free Code Snippets plugin or a child theme. Install Code Snippets, open Snippets, then Add New, paste the code, and click Save and Activate. Never paste PHP into a parent theme, a plugin file or WordPress core files, and back up first.
Key takeaways
- The two safe places for a snippet are the Code Snippets plugin and a child theme's functions.php file.
- Code Snippets has been on WordPress.org since 2012 and is active on 1 million+ installs.
- Code Snippets Safe Mode stops all snippets from running so you can log in and fix a bad snippet.
- A child theme's functions.php loads before the parent's, so a parent update never removes your code.
- WPCode runs on 3 million+ sites with a 4.9 out of 5 rating, and its paid plans start at $49 per year.
What is the safest way to add a code snippet in WordPress?
Use the free Code Snippets plugin or a child theme, and never paste PHP into your parent theme, a plugin file or a WordPress core file.
Those two safe places keep your code through theme and plugin updates, and both let you switch a snippet off again if it breaks your site. SiteOrigin's guide to adding code snippets names the Code Snippets plugin and a child theme as the two safe places, and warns that theme and plugin updates overwrite edits to their files.
A bad PHP snippet can stop your site from loading, so the safety comes from where the code lives and how you can undo it. Back up your site first, then try the snippet on a staging copy if your host offers one.
How do you add a snippet with the Code Snippets plugin?
Install Code Snippets, open Snippets, then Add New, paste the code, pick where it runs, and click Save and Activate.
Follow these steps:
- Go to Plugins, then Add New, search for Code Snippets, and install and activate it.
- Go to Snippets, then Add New.
- Name the snippet for what it does, such as “Link login logo to site”.
- Leave the type set to Functions, which is PHP.
- Paste the code into the code field.
- Leave Run everywhere selected unless the snippet's instructions say otherwise.
Then save and switch it on:
- Click Save and Activate.
If the snippet starts with <?php, leave that line out; the plugin removes it when you save. SiteOrigin's guide to adding code snippets gives these same steps and notes that the plugin checks a PHP snippet for errors when you activate it.
Code Snippets has been on WordPress.org since 2012 and is active on 1 million+ installs, as stated on its WordPress.org plugin page. The free plugin covers PHP snippets and reusable HTML content; CSS and JavaScript snippet types are part of the Pro version.
How does Safe Mode save a site a snippet breaks?
Safe Mode stops all snippets from running so you can log in, find the bad snippet, and turn it off.
A PHP error in a snippet can lock you out of wp-admin, which is the moment Safe Mode earns its keep. Code Snippets includes Safe Mode and feedback tools for fixing snippet failures, as stated on its WordPress.org plugin page.
The plugin page also warns that Safe Mode helps with recovery but does not make every snippet safe or guarantee that every error can be prevented. It is a recovery tool, not a promise that bad code cannot hurt your site.
Keep FTP or file manager access to your host as a second way back in. If Safe Mode does not restore access, you can rename the plugin folder to turn the plugin off, then log in and fix the snippet. The guide to backing up WordPress covers the backup you should have before any of this.
How do you add a snippet to a child theme?
Put the code in the child theme's functions.php file, which WordPress loads before the parent theme's functions.php.
A child theme can have its own functions.php file, and adding a function there is a safe way to change a parent theme, since the parent's update does not remove it. That is stated in the WordPress Theme Handbook's theme functions page.
The same page notes that functions.php behaves like a WordPress plugin, adding features to your site, and that only the active theme's functions.php runs. If you switch themes, the snippet stops running, so a child theme suits code that belongs with one theme's design.
Use a child theme when you already have one and the snippet changes that theme. If you have no child theme yet, the plugin route is faster than building one. The guide to what a child theme is explains the child theme setup.
How do you add a snippet with WPCode?
Install WPCode, open Code Snippets, then Add Snippet, pick the snippet type, paste the code, and set where it runs.
WPCode is the most installed snippet plugin, running on 3 million+ WordPress sites with a 4.9 out of 5 rating, according to WPCode. It handles PHP, JavaScript, CSS, HTML and text snippets, plus header and footer code, from one screen.
The free version covers the main features, and paid plans start at $49 per year, as stated on the same WPCode page. Paid plans add items such as a cloud snippet library, scheduled snippets and AI snippet generation.
WPCode also ships a library of ready-made snippets you can import with one click, which helps when you need a common task done and do not want to write the code yourself. Read any library snippet before you activate it, just as you would code from any other source.
Which place should each kind of snippet go?
PHP that changes how the site works goes in Code Snippets or WPCode; CSS goes in Additional CSS or a snippet plugin; header and footer scripts go in WPCode's Header and Footer boxes.
| Snippet type | Best place | Why |
|---|---|---|
| PHP function or hook | Code Snippets plugin | On and off switch, survives theme changes |
| PHP tied to one theme | Child theme functions.php | Keeps code with that theme's design |
| Tracking script | WPCode Header and Footer | Loads in the head or before the closing body tag |
| Custom CSS | Additional CSS field | Built into WordPress, survives theme updates |
| HTML content reused often | Code Snippets HTML snippet | Insert with a shortcode in posts and pages |
Code Snippets stores snippets apart from your theme in the database, so changing theme or updating WordPress does not remove them, as its WordPress.org plugin page states. Code that depends on a theme or plugin may still need updating when that dependency changes.
CSS is a special case: the free Code Snippets plugin does not offer a CSS snippet type, while its Pro version does. For a few CSS rules, WordPress's own Additional CSS field is the simpler choice.
How do you check a snippet before you activate it?
Read the code, check that you understand what it changes, test it on staging, and make sure function names cannot clash with names already in use.
Only use code you trust, review it before activation, and keep a current backup. That warning is printed on the Code Snippets WordPress.org plugin page, and it applies to every snippet plugin, not just that one.
Check these points before you click Activate:
- The snippet comes from a source you trust, and its notes explain what it changes.
- Function names use a prefix based on your site, such as
mybakery_, so they cannot clash with a function of the same name elsewhere. - The code has no
eval, remote file fetching or database writes you did not expect. - You have a fresh backup and a staging copy to test on.
If the notes do not explain what the code changes, ask the author before you activate it. SiteOrigin's guide to adding code snippets gives the same advice: use code from a source you trust, back up, and try it on staging first.
How do you remove or turn off a snippet?
Open the Snippets list, switch the snippet off, and delete it only once the site works fine without it.
Each snippet in Code Snippets has its own on and off switch, so turning one off is a single click and the code stays saved for later. The plugin also lets you lock key snippets to stop accidental edits or deletion, as stated on its WordPress.org plugin page.
In a child theme, turning a snippet off means editing functions.php and removing the code, since there is no switch. Keep a copy of the removed code in a note, in case you need it again.
If a snippet was added to the wrong place, such as the parent theme's functions.php, move it to a safe place before the next theme update deletes it. A staging copy is the right place to check the move; the guide to setting up a staging site for WordPress shows how to build one.
What mistakes should you avoid?
Do not paste code you do not understand, do not edit parent theme or plugin files, and do not skip the backup.
The three files you must never edit for snippets are the parent theme's functions.php, any plugin's files, and WordPress core files. SiteOrigin's guide to adding code snippets lists all three, since updates replace them and your code is lost.
Other mistakes to avoid:
- Activating a snippet on the live site without testing it on staging first.
- Using function names with no prefix, which can clash and stop the site from loading.
- Leaving a broken snippet active while you guess at a fix.
- Installing a snippet plugin and then still editing theme files out of habit.
Before you add any snippet, confirm you know what it does, where it lives, and how you will turn it off. If any of those three is unclear, stop and find out first. The guide to checking a plugin for vulnerabilities is worth reading before you install any new plugin, including a snippet manager.
Frequently asked questions
Which code snippet plugin should I use?
Code Snippets and WPCode are the two most used options. Code Snippets has 1 million+ active installs and covers PHP and HTML in its free version. WPCode has 3 million+ installs and covers PHP, JavaScript, CSS, HTML and text. Pick one and use it for all your snippets.
What do I do if a snippet breaks my site?
Open the Snippets list and switch the snippet off. Code Snippets also has a Safe Mode that stops all snippets from running so you can log in and fix the problem. If Safe Mode does not restore access, rename the plugin folder through your host's file manager.
Can I add a snippet to functions.php?
Yes, but only in a child theme's functions.php file, never in the parent theme's file. A child theme's functions.php is loaded before the parent's, and a parent theme update does not remove it. If you switch themes, the snippet stops running.
Can the free Code Snippets plugin handle CSS?
CSS and JavaScript snippet types are part of Code Snippets Pro, not the free plugin. For a few CSS rules, use WordPress's own Additional CSS field instead. WPCode's free version does cover CSS and JavaScript snippets.
Is it safe to paste PHP code I found online?
Only code from a source you trust, with notes that explain what it changes. Check for a function-name prefix, unexpected database writes and remote file fetching. Back up your site and test the snippet on a staging copy before you activate it on the live site.
Do my snippets survive a theme change?
Yes. Code Snippets stores snippets apart from your theme in the database, so changing theme or updating WordPress does not remove them. Code that depends on a theme or plugin may still need updating when that dependency changes.
Sources
- Code Snippets WordPress.org
- Theme Functions WordPress Developer Resources
- Add Code Snippets to WordPress Safely SiteOrigin
- 6 Best WordPress Code Snippet Plugins for Safer Code WPCode
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.
Related guides
About the editors
Researched from the primary sources listed above and checked against our editorial rules before publishing.


