From time to time, you might need to add a small amount of code to your site to modify a plugin’s behaviour or output. There are two recommended ways to do this:
Please read the Troubleshooting step below before implementing either of these methods.
Use a snippets plugin
You can install a plugin specifically designed to allow you to add snippets. The advantage of this is that you don’t need to edit theme files, you can add the snippets direct in your WordPress dashboard.
I recommend the free Code Snippets plugin for adding snippets to your site. This is the easiest and best method.
Via your child theme’s functions.php file
If you are using a commercial theme, you should have created a child theme. This will allow you to add new code and modify the theme without the fear of your changes getting overwritten the next time the parent theme is updated.
You can create a child theme in a couple of different ways:
Whatever method you use, you should always add any code snippets to the functions.php file of your child theme, not the parent theme.
Troubleshooting
This snippet is written in PHP – the coding language that WordPress runs on. When you are adding this snippet using either of the methods above, note that the snippet begins with the PHP tag <?php
.
Please note that you might not need to include this tag when you add the snippet.
All your PHP code needs to be inside <?php
and ?>
tags. If your functions.php or snippets file already has an opening <?php
tag then you don’t need to add another one.
If you need more information, please read through this article, particularly the section on Troubleshooting.