Often while reading WordPress tutorials, you may be asked to add custom code snippets in your theme’s functions.php file or in a site-specific plugin. The problem is that even a slightest mistake can break your website. In this article, we will show you an easy way to add custom code in WordPress without breaking your site.
The problem with Custom Code Snippets
Often you’ll find code snippets in WordPress tutorials with instructions to add them into your theme’s functions.php file or a site-specific plugin.
The problem is that even a tiny mistake in the custom code snippet can break your WordPress site and make it inaccessible.
The other problem is that if you add multiple code snippets in a site-specific plugin, then it can become hard to manage the file.
Recently while looking for a solution, we discovered an easy for users to add and manage custom code snippets in WordPress.
Adding Custom Code Snippets in WordPress
First thing you need to do is install and activate the Code Snippets plugin on your website. For more details, see our step by step guide on how to install a WordPress plugin.
Upon activation, the plugin will add a new menu item labeled Snippets into your WordPress admin bar. Clicking on it will show you a list of all the custom code snippets you have saved on your site.
Since you just installed the plugin, your list will be empty.
Go ahead and click on the Add New button to add your first custom code snippet in WordPress.
This will bring you to the ‘Add New Snippet’ page.
You need to start by entering a title for your custom code snippet. This could be anything that helps you identify the code. After that you can copy and paste your code snippet into the code box.
In the screenshot above, we have added a custom code snippet to remove the WordPress version number from our test site.
function wpb_remove_version() { return ''; } add_filter('the_generator', 'wpb_remove_version');
Below the code box, you will see a text area to add description. You can add anything here that helps you understand what this code does, where you found it, and why you are adding it to your website.
You can also assign tags to your code snippet. This will help you sort your code snippets by topic and functionality.
Finally, you can click on the ‘Save Changes and Activate’ button.
If you just want to save the code snippet and not activate it, then you can click on the ‘Save Changes’ button.
You can also change the scope of the code snippet. You can run it only in the WordPress admin area, on the front-end of your site, or everywhere. If you are unsure, then keep the default ‘Run snippet everywhere’ option checked.
Once you have saved and activated the code snippet, it would become effective on your site immediately.
Handling Errors in Custom Code
Normally, if you make a mistake in adding the custom code in your site-specific plugin or theme file, it would immediately make your site inaccessible.
You’d start seeing a syntax error or a 500 internal server error on your site. To fix this you’ll need to manually undo your code using an FTP client.
The neat part about the Code Snippets plugin is that it will automatically detect a syntax error in the code and immediately deactivate it.
It will also show you a helpful error message, so you can debug the error.
Managing Your Custom Code Snippets
Code Snippets plugin provides an easier graphical user interface to manage your custom code snippets in WordPress.
You can save code snippets without activating them on your site. You can deactivate the snippet at any time you want. You can also export specific code snippets or bulk export all of them.
If you’re moving websites to a different server, you can easily import your code snippets using the Code Snippets plugin by visiting Snippets » Import page.
Code Snippets plugin work out of the box, but you can tweak the plugin settings by visiting the Snippets » Settings page.
We hope this article helped you learn how to easily add custom code in WordPress. Want to experiment with some code snippets on your website? Check out our list of extremely useful tricks for the WordPress functions file.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Hi and thank you so much for all your work and support to the WordPress community.
I used Code Snippets plugin and it was just perfect until after last update, when all code disappeared, and it became impossible to save any change: the result is a 404 error page.
You could try the troubleshooting steps in our article here: https://www.wpbeginner.com/beginners-guide/beginners-guide-to-troubleshooting-wordpress-errors-step-by-step/
to see if it could be a conflict with another plugin or theme
Thankyou for posting this. It works
Glad our article could help
I have a long Authors page with affiliate links and want to have users be able to pull up those books for certain topics.
For instance, say that they are books covering Gardening, Cooking, and Sewing. I want the user to be able to pull up all the books on Sewing without having to see all the other titles. Plus some titles do not tell you immediately the subjects so browsing the entire list might not catch them all.
I want to be able to code the individual titles so they would appear on one or more of the predetermined subject lists and then provide a link to the user to click to get a dynamic list of just the titles on. that particular subject Dynamic such that any new titles I add and code will show up for them.
I am NOT a programmer, don’t want to be, I need a plugin that will do that. Seems that I am probably not the first person wanting to do this. Is there a plugin that will do this?
Hi
i am trying to have a function code added to the checkout page only , so how do I use the snippet to do so?
Can I apply code snippets to all pages EXCEPT my homepage?
I tried using this plug in because everyone seems to love it, but it crashed my whole site before I could even do anything with it.
This article convinced me to try it again, because everyone seems to like it, but EVEN JUST ACTIVATING it wrecks my whole site and I get the 500 error you refer to.
Do you have any idea what might be going on?
I would assume it’s my error but all I am doing is installing it and activating it.
Hi,
This could be due to a conflict with another plugin, theme, or custom code on your website. Please see our guide on troubleshooting WordPress errors to figure out what’s causing this issue.
Would the code entered into this plugin persist through theme updates? Here’s what I’d like this plugin to do: In order to have the year update automatically in the copyright, many developers place a snippet of php code into a child theme to accomplish this. I really don’t want to make a child theme for every website just for that tiny change, but the alternative is to have my changes disappear with each theme update. But adding a child theme for something so minor seems like building a whole new fence when all you want to do is change the color of the latch on the gate. Maybe this plugin is the easy way to do this?
Theme updates won’t affect your code.
I’m an old PHP developer, but this makes managing snippets a lot easier. (And, since I’m human, I made an error in adding a snippet – and it was just deactivated, Doing it manually would have meant making sure that the editor was still open when I tried the site, so I could go back and try to find what would have been a 500 error.
Thank you for this post.
What is the scope of this snippet?
Can this snippet code override any main theme functions?
Thanks for sharing this.I am also afraid of adding custom code.But this looks easy.If I understood correctly if the code snippet has an error the plugin automatically decativates it .Right?
Hey Amrita,
It tries to and can catch most common syntax errors, but sometimes it may fail to check. It all depends on the code you are trying to add.
Thanks for this great plugin. Now I can manage custom snippets more easily.