How to Add and Use PHP Code Snippets in WordPress
Everyone starts as a beginner. We all deserve clear, simple, and straightforward explanations instead of complicated technical jargon. In this guide, I’ll show you everything you need to safely and easily add code snippets to your WordPress site.
First, what exactly is a “code snippet”?
A code snippet is just a short piece of PHP code you add to your website. It can help you add new features, remove unwanted ones, improve existing functionalities, or modify how things work on your website. Simply put, snippets make your WordPress site better and more personalized.
Here’s a little example: this code makes the shortcode display the current year—perfect for a site footer.
// Shortcode [year]
function sexy_current_year_shortcode( $atts ) {
return esc_html( date( 'Y' ) );
}
add_shortcode( 'year', 'sexy_current_year_shortcode' );Two Easy Ways to Use Code Snippets
You have two main options to add code snippets safely to your WordPress website:
1. Using a Snippet Plugin (Recommended)
The safest and easiest method is using a snippet manager plugin. This type of plugin lets you add, edit, or remove PHP snippets right from your WordPress dashboard.
To use a snippet plugin:
- Log in to your WordPress admin area.
- Click on “Plugins” → “Add New”.
- Type “code snippets” into the search bar.
- Choose a reliable and popular plugin (e.g. “Code Snippets” or similar), install and activate it.
- After activation, you’ll have a new menu where you can add snippets directly.
The snippet manager will save your snippets in a database. If something goes wrong, you can quickly deactivate the snippet right from your WordPress dashboard.
2. Adding Snippets Manually to Your Theme’s functions.php File
The second way is by manually adding snippets to your theme’s functions.php file. It’s easy to do but carries a slightly higher risk, especially if you make mistakes.
To do this:
- Log in to your web hosting control panel or use FTP (a simple way to access your website files).
- Navigate to wp-content/themes/your-theme-name/.
- Find the file named functions.php and open it in a file editor.
- Paste your PHP snippet at the end of the file.
- Save your changes and upload the updated file.
Note: Changes made directly to your theme’s functions.php file can get lost when the theme updates. Also, a small typo could temporarily break your site, requiring you to remove or correct the snippet using FTP or your file manager.
Changes not visible? Clear your cache!
Sometimes, after adding a code snippet, it might seem like nothing changed on your website. Don’t worry—it usually means your website or browser is showing a cached (old) version.
Caching helps your website load faster by storing temporary copies of files. But this means you won’t immediately see changes you’ve just made.
To fix this, clear your cache:
Website Cache
- First, clear the cache provided by your hosting service. You can usually do this from your hosting control panel.
- If you have a caching plugin installed (like LiteSpeed Cache, WP Super Cache, WP Rocket, or similar), log in to your WordPress dashboard, find the cache plugin’s menu, and select the option to clear or purge the cache.
Browser Cache (Hard Reload)
After clearing your website cache, it’s good practice to clear your browser cache as well:
- Chrome (Windows/Linux): Press Ctrl + F5
- Chrome (Mac): Press Cmd + Shift + R
- Firefox (Windows/Linux): Press Ctrl + F5
- Firefox (Mac): Press Cmd + Shift + R
- Edge (Windows): Press Ctrl + F5
- Safari (Mac): Press Cmd + Option + R
- Brave (Windows/Linux): Press Ctrl + F5
- Brave (Mac): Press Cmd + Shift + R
Doing a hard reload forces your browser to fetch the newest files and updates, making sure you see your recent changes clearly.
Now your changes should appear correctly!
Which Method Should You Choose?
Both methods have their advantages and disadvantages:
- Snippet Plugin (recommended):
- Easier and safer, especially for beginners
- Quick to fix issues (disable problematic snippets from WordPress admin)
- Snippets stay even when updating themes or plugins
- php Method:
- Easy to implement without extra plugins
- Riskier—errors can temporarily break your site
- You lose changes if you update or change your theme
For most users, using a snippet plugin is strongly recommended.
Always, Always Back Up First!
Before you make any changes or add snippets to your WordPress website, you absolutely need to make a backup. Trust me, doing backups regularly makes life easier and calmer.
When testing a snippet, sometimes things may go wrong and your site could temporarily crash. Don’t panic. If you’re using a snippet plugin, you can simply deactivate the snippet from your WordPress admin dashboard.
If your snippet plugin causes your site to go down completely, log into your hosting control panel, access the database through your hosting’s phpMyAdmin tool, and delete the problematic snippet from the database manually.
Make sure your hosting login information is always ready and handy. Always be prepared, and always have recent backups.
Written by Milan on 11th July 2025

