There are two ways to add PHP code to WordPress. You can either use a plugin or add the code directly in the functions.php file of your theme. Let’s look at both the options here.
Add PHP Code to WordPress using a Plugin
You can use a plugin to easily add PHP Code to your WordPress website. This is the method I recommend for beginners. It’s simple and straightforward.
And I personally use (and highly recommend) Code Snippets Plugin to get the job done:
You can simply install this plugin from WordPress Plugin Repository.
Once installed, click on the ‘Snippets’ from the left admin bar menu > then click on ‘Add New’ at the top.
And next, you can give your code a title (for your reference) and simply paste the PHP code and click ‘Activate’.
Add PHP Code to WordPress using functions.php
If you are using a child theme and know what you are doing, you can follow this approach.
First click on ‘Appearance‘ > ‘Theme File Editor‘
Next, click on ‘functions.php’ and add your code in this file.