Use code BLACKFRIDAY through Dec. 2nd for 30% off!
  1. Home
  2. Docs
  3. Tips
  4. How to add PHP to WordPress (quick and easy)

How to add PHP to WordPress (quick and easy)

WordPress core, themes, and plugins do most of the work on your website. But there are usually times when you need to add a code snippet and or PHP to WordPress. Usually, this is to add a filter to manipulate or change how a plugin or theme will work. For example, perhaps you to extend Perfmatters with one of our more advanced features or build an integration. Below are a few easy ways you can do this without crashing your site.

How to add PHP to WordPress with Perfmatters

The first option is to add PHP to WordPress using Perfmatters. This is by far the easiest and safest method, as we already have a Code Snippet feature built right in. You don’t have to edit WordPress core, your theme, or a plugin. The code will remain throughout any updates on your site, and you don’t have to worry about messing with a child theme. We ditched child themes years ago as they simply complicate things.

Follow the steps below.

Step 1

Click into the Perfmatters plugin settings.

Perfmatters plugin settings
Perfmatters plugin settings

Step 2

Click on the “Code” menu.

Code menu
Code menu

Step 3

Click on “New Snippet” in the header.

New snippet
New snippet

Step 4

On the individual snippet page, give your snippet a name and choose the type of snippet. In this case, the default should already be PHP.

Then add your snippet of PHP code in the “Code” box. In this example, we’re adding a filter from our Perfmatters plugin to change the fade in speed on lazy loading.

PHP code snippet in Perfmatters
PHP code snippet in Perfmatters

Step 5

Under the “Location,” choose where you want the code snippet to run. In this case, we only need it to run on the front end of the site. Although with most PHP code snippets, you’ll probably need it to run everywhere. If so, you can just leave the default.

PHP code snippet location
PHP code snippet location

Step 6

If you want the snippet to load right away, toggle on “Active” in the top right, and then click “Save Snippet.”

Activate and save snippet
Activate and save snippet

How to add PHP to WordPress with a free plugin

The second option is to add PHP to WordPress using a different free plugin. Follow the steps below.

Step 1

Install the free WPCode plugin. It’s actively maintained and has a five-star rating, with over 1,600 five-star reviews and 2+ million active installs. That’s saying something.

WPCode WordPress plugin
WPCode WordPress plugin

If you want a few alternative plugins, you can also check these out:

Step 2

Under “Code Snippets” in your WordPress admin sidebar, click on “+ Add Snippet.”

Add code snippet
Add code snippet

Step 3

Under “Add your Custom Code (New Snippet),” click on “Use snippet.”

Add your custom code
Add your custom code

Step 4

Add the name of your code snippet, this can be anything you want. Then add your snippet of PHP code. In this example, we’re adding a filter from our Perfmatters plugin to change the fade in speed on lazy loading.

Code snippet name
Code snippet name

Step 5

Choose “PHP Snippet” from the dropdown. Then, make sure your code is properly formatted. But don’t worry—if it isn’t, the plugin will give you an error and not let you apply it, which is good. It means it won’t crash your site.

WPCode PHP code snippet
WPCode PHP code snippet

Step 6

Under the “Insertion” section, choose where you want the code snippet to run. In this case, we only need it to run on the front end of the site. Although, with most PHP code snippets, you’ll probably need it to run everywhere. If so, you can just leave the default.

Select where code snippet should run
Select where code snippet should run

Step 7

If you want the snippet to load right away, toggle on “Active” in the top right, and then click “Save Snippet.”

Save snippet
Save snippet

How to add PHP to WordPress manually

The second option to add PHP to WordPress is manually, by adding the code to your theme’s functions.php file. Just be careful, this can break a site if you’re not careful. Follow the steps below. 

If you’re using a theme based on full-site editing, we recommend the plugin method above, as things have changed quite a bit.

Step 1

Under “Appearance” in your WordPress admin sidebar, click on “Theme File Editor.”

WordPress theme file editor
WordPress theme file editor

Step 2

Click into your theme’s functions.php file. If you are using a child theme, then you would want to use that instead.

WordPress functions.php file
WordPress functions.php file

Step 3

At the bottom of the file, input your PHP code. In this example, we’re adding a filter from our Perfmatters plugin to change the fade in speed on lazy loading.

Make sure your code is properly formatted, otherwise you can break your site. 

Add PHP function to functions.php file
Add PHP function to functions.php file

The good news is that newer versions of WordPress do have some built-in safeguards now. Here is an example below. We forgot to close a statement, and after updating the file, it rolled back the changes when it detected the problem. This kept the WordPress site from crashing. In the good ole days, this would have caused a white screen of death.

WordPress code changes rolled back
WordPress code changes rolled back

Step 4

Click “Update File.”

As you can see, adding PHP code to your WordPress site doesn’t have to be complicated. The free plugin method actually makes it super quick, safe, and easy!

Related Articles