You need to add custom PHP code to WordPress sites sometimes. To be more specific, you have to add functions, hooks, or custom PHP code to the function.php file of your WordPress sites. So, how can you do it? Well, there are several ways you can add PHP code to WordPress sites. But any unwanted mistake will break your website. This is why following a detailed guideline is always recommended.
In this tutorial today, we will show you the easiest ways to add custom PHP code to WordPress sites. In fact, we will show you a couple of ways so that you can follow the easy and safe one for your site.
So without further delay, let’s get started right away.
How to Add Custom PHP Code to WordPress Sites Manually.
Adding functions, hooks, or any other custom PHP code to the WordPress site is easy. WordPress has a default editor to modify code of your themes and plugins. And you can also add PHP code with a plugin. Let’s start with the manual system.
Login to your WordPress site, go to Appearance > Theme Editor. Initially, it warns you because you are making direct edit to your theme file. Click on I Understand and you will be taken to the theme editor.
Select the function.php file on your left and place your PHP code at the bottom. Finally, click on “Update File” finish up adding code to the function.php file. This way you can edit other theme files too.
Note: WordPress does not recommend editing files on the default editor this way. You may run into errors and end up breaking your site. And every time you update your theme, any modification you made to these files will be gone. You have to add code again after every update.
How to Add Custom PHP Code to WordPress Sites With a Plugin
You can add functions, hooks, or other custom PHP code to WordPress sites with a plugin too. And that is the safest way. Because, in this case, you will add PHP code without editing files. And you no need to do it again and again after every update. We will do it with My Custom Functions WordPress plugin.
Step 1: Install and Activate My Custom Functions Plugin
When you are logged in, Plugins > Add New on your Dashboard. Search My Custom Functions in the search and click Install and Activate subsequently.
Step 2: Add Custom PHP Code to Your WordPress Site
Once you have installed and activated My Custom Functions plugin. You need to go to Settings > PHP Inserter on your Dashboard.
Place your code under “Functions (PHP code)” and turn on the toggle on your left. Click on Save Changes to make your code work.
Note: Do not wrap your custom PHP code in HTML tags, such as <?php…?>. If your code has no error it should work flawlessly. And in case of any website crash follow the author’s instructions.
Warning: Even missing a single semicolon at the end of your code can break your site. Please be careful while writing code. It is best to try your code on a WordPress site installed on your local computer and make sure it gives you the desired result. Then add your code to the live site. Again please be careful.
Though we have described 2 detailed methods here in this tutorial, we recommend you to add custom PHP code to WordPress sites with a plugin. It’s because WordPress does not recommend users to make direct edit to WordPress site files. And even if you do so, you will need to add code after every time your theme gets updated. It’s a continuous hassle, and you might forget it sometimes. Going with the second method will be your best decision. Please do visit Virfice for more of such tutorials. Good luck.