How to Add PHP Code to WordPress
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:
Code Snippets
You can simply install this plugin from WordPress Plugin Repository.
Once installed:
- Click on the 'Snippets' from the left admin bar menu
- Click on 'Add New' at the top
- Give your code a title (for your reference)
- Paste the PHP code
- 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
Note: Make sure you're using a child theme before editing functions.php directly. This ensures your changes won't be lost during theme updates.