How to Add PHP Code to WordPress

Srivishnu Ramakrishnan
Srivishnu Ramakrishnan
Engineer & Web Creator
LinkedInTwitter

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:

  1. Click on the 'Snippets' from the left admin bar menu
  2. Click on 'Add New' at the top
  3. Give your code a title (for your reference)
  4. Paste the PHP code
  5. 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.

  1. First click on Appearance > Theme File Editor
  2. 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.