We occasionally ask our users to add custom code to their WordPress website to add simple features to GiveWP. We do this because 80% of GiveWP users don’t need that feature so we don’t want to add it to the core plugin. But where and how exactly are you supposed to add this custom code to your website? This article gives you an overview of why and how to do this right.
Dispelling the Myths of Custom Code in WordPress
Experienced developers know the best way to extend the functionality of a plugin is by adding their own code into what’s often called a “Functionality Plugin“. You might think that you can simply go into the plugin files and change them there. While it is true that you CAN do that, it’s not “best practice” because the next time your plugin is updated you will lose that custom code.
Another thing you’ll often hear is that you should just put this code into your theme’s `functions.php` file. Again, while this will work, it’s not considered “best practice” because your site most likely will update it’s theme in the near future and again you’ll lose that added functionality to the plugin once your theme changes.
So, the GiveWP Support team gave you some custom code to use on your website, or you pulled it from our documentation. But you CAN’T change the plugin, you can’t add it to your theme’s `functions.php` file, and you don’t know how to create a Functionality Plugin. What are you supposed to do?
It is not "Best Practice" to put custom plugin code in your theme's functions.php file.
Use the “My Custom Functions” plugin
The “My Custom Functions” plugin is an easy and safe way to insert custom functions into your WordPress website without having to learn how to use a Custom Functionality plugin. Here’s the overview:
- Install My Custom Functions just like any other WordPress plugin
- After it is activated, go to “Settings > PHP Inserter” ( for plugin version greater than or equal to 4.10 ) else, go to “Appearance > Custom Functions” ( version less than 4.10 )
- There you’ll see a textarea where you can add custom functions.
- Make sure you switch the toggle to ON and save changes
In the example below, I took some code from our documentation on making custom fields required, and inserted it into the textarea.
If you’re taking snippets from our Snippet Library, on the first line of the snippet, you’ll see and opening <?php tag. Make sure that when you copy/paste the snippet into My Custom Functions, you do not copy that first line that includes <?php to prevent PHP errors.
If you need to add more than one snippet, you can insert a line break in My Custom Functions, then paste the second snippet below the first.
It’s important that there be at least one line break in between the two snippets. Notice how below, line 12 is left blank, then the second snippet is copy/pasted below it.
One really helpful feature of this plugin is that if you insert a function into the field and hit “Update” but there is something wrong with the function, it will disable it automatically and give you an alert like this:
With Great Power Comes Great Responsibility
We provide this documentation in order to help our GiveWP users to add custom functions. Please be aware that this type of customization can create conflicts or issues on your website. We always highly recommend that you add custom functions in an environment that allows you to quickly undo what you’ve done without causing issues your live site. We also recomend you test thoroughly before going live. Here are a few important things to keep in mind