How to add a code snippet to your WordPress site
Some of our documentation or tutorials may ask you to add a code snippet to your site to alter the behavior of our plugins. We explain the 2 recommended ways to do this below.
1. Via the functions.php file
If you know your way around WordPress and are not afraid to edit theme files, this option is preferred.
If you are using a commercial theme, you should have created and activated a child theme. This will allow you to add new code and modify the parent theme without the fear of your changes getting lost the next time the parent theme is updated.
So if you have a child theme active, the best way to add code snippets is through the child theme’s
functions.php file. You can edit this file through FTP (preferred) or via the WordPress dashboard
Appearance > Theme editor.
You should add code snippets to the bottom of that file. If the bottom of your functions.php file has a line that has
?>, then you should add the code snippet right before that line.
IMPORTANT NOTE: as explained, this method is not preferred if you do not have a child theme active! Either install a child theme or move on to option 2.
2. Via a 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. Instead, you can add the snippets directly from your WordPress dashboard. We recommend using the free Code Snippets plugin.
After you’ve added the code
If you use a caching plugin, make sure to clear the cache when you’ve added custom code to your site via either of the options explained above.
Was this article helpful?