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.
Install the free Code Snippets plugin. I’ve used this plugin for over five years and never had a single issue. That’s saying something. It also has a five-star rating, with over 300 five-star reviews.
Under “Snippets” in your WordPress admin sidebar, click on “Add New.”
Click on the “Functions (PHP)” tab.
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.
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.
Select 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.
Click “Save Changes and Activate.”
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.
Under “Appearance” in your WordPress admin sidebar, click on “Theme File Editor”
Click into your theme’s
functions.php file. If you are using a child theme, then you would want to use that instead.
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.
Click “Update File.”
As you can see, adding PHP code to your WordPress site doesn’t have to be complicated. The free Code Snippets method actually makes it super quick, safe, and easy!
Have any questions? Feel free to drop them in the comments below.