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. Today I’ll share a few easy ways you can do this without crashing your site.
How to add PHP to WordPress with a plugin
The first option is to add PHP to WordPress using a free plugin. This is by far the easiest and safest method. 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. I ditched child themes years ago as they simply complicate things.
Follow the steps below.
Step 1
Install the free WPCode plugin. I’ve used this plugin for years and never had a single issue. That’s saying something. It also has a five-star rating, with over 1,200 five-star reviews.
Step 2
Under “Code Snippets” in your WordPress admin sidebar, click on “+ Add Snippet.”
Step 3
Under “Add your Custom Code (New Snippet),” click on “Use snippet.”
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, I’m adding a filter from our Perfmatters plugin to change the fade in speed on lazy loading.
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.
Step 6
Under the “Insertion” section, choose where you want the code snippet to run. In this case, I 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.
Step 7
If you want the snippet to load right away, toggle on “Active” in the top right, and then click “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 function.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”
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.
Step 3
At the bottom of the file, input your PHP code. In this example, I’m 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.
The good news is that newer versions of WordPress do have some built-in safeguards now. Here is an example below. I 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.
Step 4
Click “Update File.”
Summary
As you can see, adding PHP code to your WordPress site doesn’t have to be complicated. The free WPCode method actually makes it super quick, safe, and easy!
Have any questions? Feel free to drop them in the comments below.