By default, WordPress does not allow running PHP code in posts or pages. However, In some cases, you may need to add a function to your website. Well PHP is unforgiving. A small error can break your whole website. So let’s see how to safely add PHP to your WordPress website.
Code Snippets Plugin
The Code Snippets plugin is the best plugin to add PHP snippets to your WordPress website. The best thing about this plugin is that you can activate or deactivate your PHP snippets. And your snippet will not be activated if there is an error.
Installing the plugin.
To install it, go to Plugins > Add New and search for Code Snippets. And simply install and activate the plugin.
Create your own plugin file!
Yes!! Another way is to create your own plugin. It’s not that tough to create a simple plugin.
But, if it is difficult for you, you can actually use a plugin to create another plugin very easily.
Pluginception Plugin is the one you need. To install it, go to Plugins > Add New and searching for Pluginception. And simply install and activate the plugin.
Now that it’s activated, go to Plugins > Create New Plugin.
Name it something super specific to you so it doesn’t conflict with any other plugins, and create it.
Now you are ready to add your functions to the plugin file.
Using Child Theme
Using a child theme is another safe option for you. Read this article for full details.