Whether you’re a professional or have just started customizing your WordPress site, it’s important to know where to put your custom code / code snippets.
It can make a big difference to performance and maintenance.
In this post, we’ll discuss three ways to add custom code to your site, so you can choose the method that best suits you and your specific situation.
Let’s take a look at the options.
1. Use a code snippets plugin
The Code Snippets plugin is a good option for those who are less familiar with code.
The plugin is easy to use, free to install and managed directly from the WordPress admin area.
With this plugin, you can easily add code snippets and enable or disable them as needed, without worrying about errors.
> To get started, go to Plugins > Add New Plugin and search for “Code Snippets”.
> Once you’ve installed and activated the plugin, you can go to Snippets > Add New to create a new code snippet.
Give it a name, paste in the code, and you’re done.
> In the Snippets > All Snippets section, you’ll find a toggle that lets you enable and disable specific snippets as needed.
The plugin also makes it easy to export code snippets so you can import them into another WordPress website later.
A Cloud version will also be available for this in the future, allowing you to easily share code snippets with other WordPress sites.
Although this is the easiest way to use PHP code snippets on your WordPress website, there are other options that require a little more knowledge, such as connecting to the website via FTP.
2. Create your own plugin for your code snippets
This method is more geared toward developers and offers a number of advantages, such as the fact that the snippets are not stored in a database, but are instead directly in the code where they are intended to be. To use this method, simply create an empty .php file in the wp-content/plugins folder. The contents of the file should look like this:
<?php
/*
* Plugin name: Naam van jouw code
* Description: Omschrijving van wat de code doet
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Onderstaand kun je jouw code plaatsen
3. Add it to your theme’s functions.php file
A third way to add custom code to your WordPress website is to place it in your active theme’s functions.php file.
This file is specifically for adding or modifying functionality on your WordPress website, such as adding new shortcodes, creating widgets, or creating custom functions.
It is important to bear in mind that the code you add to the functions.php file runs on every page of the site.
This can affect your site’s performance, so it is advisable to add only the code that is really necessary.
When you change or update the theme, the code you added to functions.php may be lost.
Therefore, we recommend that you also save any code you add in an external file so that you can easily restore it when necessary.
If you do not have much experience working with code, we recommend that you first make a backup of the current functions.php file so that you can restore it in case of an error.
And if you are not sure what you are doing, it is advisable to seek professional advice before making any changes to the functions.php file.



