PHP powers 78.8% of websites and is crucial for WordPress customization. This straightforward guide explains PHP basics and shows four easy ways to add or edit PHP in WordPress, enhancing your site’s functionality.

Key Takeaways

  • A basic grasp of PHP is essential if you want to implement significant changes or add new code to your WordPress site.
  • Using plugins is a hassle-free way to incorporate PHP code into WordPress without needing to edit files directly.
  • Editing PHP files manually through a child theme or FTP is best for those seeking the utmost customization and control.

📚 Table of contents:

An introduction to PHP in WordPress

PHP is a ‘general purpose’ programming language that is popular in website development. More importantly, WordPress is built using PHP. That makes it essential to have a basic understanding of how PHP works if you want to make big changes to the content management system (CMS) or to add new code to your site.

You don’t need to be a programmer to edit or add PHP in WordPress. Depending on what you want to do, it’s easy to find plenty of tutorials that will guide you through the entire process, and even provide code snippets you can use. For example, we have a full article that walks you through several changes you can make to the functions.php file.

Easy methods to add PHP in WordPress

If you’re not comfortable working with code, it can seem intimidating to edit WordPress and PHP files. However, WordPress offers several ways to add PHP code to your website, ranging from plugins to the manual approach. As long as you back up your website before making any big changes or use a staging site, you should feel free to experiment as needed.

1. Convert PHP into WordPress shortcodes

As usual, plugins offer the simplest approach for solving a specific problem in WordPress. There are several plugins you can use to add custom code to your WordPress files without having to edit them directly.

One option is to turn your PHP code snippets into shortcodes that you can easily use in the WordPress editor or widget areas. This is useful if you’re looking to add PHP to a specific post or page on your site.

To do this, you can use the free Insert PHP Code Snippet plugin:

With this plugin, you’ll be able to create custom shortcodes for PHP code snippets. You add can these snippets using the plugin, and it will generate shortcodes that you can use anywhere on your website to run those functions.

After you install and activate the plugin, you’ll see a new tab in the dashboard labeled XYZ PHP Code. Select that tab and go to PHPCode Snippets → Add New PHP Code Snippet:

Adding a new PHP code snippet

The plugin will ask you to set a “name” for your snippet, which should describe its purpose and to add its code within the PHP code field:

An example of a PHP snippet

Keep in mind that Insert PHP Code Snippet doesn’t validate the code you add. If the code doesn’t work, you’ll need to troubleshoot it on your own.

Once you add a code snippet using the plugin, it will appear under the PHPCode Snippets tab. You have options for pausing individual scripts, as well as editing, deleting, and previewing them:

Editing PHP

The shortcode for placing the snippet is available under the Snippet Short Code column. Once you preview the code to make sure it works, you can go ahead and add the shortcode anywhere on your site.

2. Add sitewide code snippets with a plugin

Another plugin that can help you add PHP in WordPress is the free Code Snippets plugin.

With this plugin, you can easily add general code snippets to your site by going to Code Snippets → Add New. When you add your PHP code snippet, you can choose between running it everywhere or only on the frontend or backend of your site:

Adding PHP with the free Code Snippets plugin

In addition to adding PHP code to WordPress, the plugin can also help you add custom JavaScript, CSS or HTML.

3. Customize your child theme in WordPress

WordPress comes with a tool called Theme Editor that enables you to edit your theme files directly from the dashboard. Ideally, you’ll do this while using a child theme, so you don’t lose any modifications that you make when you update the parent theme.

To access the theme editor, go to Appearance → Theme Editor. You’ll see a simple text editor to the left and a list of files to the right:

WordPress theme editor

The Theme Files list gives you direct access to all of your theme’s customizable files. Beneath each entry, you’ll also see its full filename. Most of the options here will be PHP files.

You can switch which theme you’re editing using the Select theme to edit drop-down menu. Moreover, you can look up documentation for specific functions using the Documentation drop-down menu beneath the editor:

The theme editor

Looking up a function will send you to its WordPress Development Codex page (if there’s one available). Keep in mind that if you’re editing a custom theme, you might not be able to find documentation for specific functions. Once you make the changes you want, you can click on Update File to save them.

If you’re specifically looking to add code snippets to your site, you’ll likely want to edit the functions.php file.

4. Use an FTP client to edit PHP files manually

The main downside of using the WordPress Theme Editor is that the editor itself is very barebones. If you’ve ever used a full code editor, then you know you’re missing out on a lot of functionality by modifying files from your dashboard.

A better alternative is to access your website’s files via FTP and use your favorite text editor to update them. To do so, you’ll need an FTP client such as FileZilla.

If this is your first time using an FTP client, you’ll need to look up your website’s login details. Your FTP credentials are not the same as the ones you use for logging into WordPress.

Traditionally, your web host will create an FTP account for your when you sign up for a plan. That account’s details should be accessible in your hosting control panel:

Accessing your FTP credentials

To connect to your website via FTP you’ll need the host address, your FTP username and password, and specific port details (if they’re available). Once you have that information, you can connect to your website using FileZilla or any other FTP client.

After accessing your website, you’ll need to find the WordPress root folder, which contains all of your site’s files. Usually, that folder is called public or public_html, or is named after your website:

Accessing your WordPress directory

Once you have access to the root folder, you can modify any of your WordPress files and add custom PHP code to them. Generally speaking, we recommend against modifying any files if you’re not sure what they do.

To edit files, you’ll need a general understanding of how the root directory is structured. Within the main folder, you’ll find site-wide configuration files. That includes the .htaccess file, which you can modify to add interesting functionality in WordPress.

Theme and plugin files can be found inside the wp-content folder. If you go to wp-content/plugins or themes, you’ll see that each theme and plugin has its own folder. If you open one of your theme’s folders, for example, you’ll be able to edit all of the same files you get access to with the WordPress Theme Editor tool.

To edit a file, right-click on it and select the View/Edit option. Doing so will open the file using your local default text editor:

Editing PHP using local text editor

When you’re done making changes to any file, save it and your FTP client will upload the new version to your server. That’s it!

Conclusion 🧐

Editing and adding PHP in WordPress is relatively simple. As long as you’re comfortable working with code, or even just copying and pasting it, you can add snippets that will enhance your site’s functionality in amazing ways.

However, whenever you’re adding code to WordPress, you should always back up your site first in case something goes wrong. For example, adding PHP with improper syntax might trigger WordPress recovery mode.

To recap, the four main ways to add PHP in WordPress are:

  1. Turning your PHP into shortcodes with the Insert PHP Code Snippet plugin
  2. Adding PHP snippets to your site with the Code Snippets plugin
  3. Customizing your child theme using the Theme Editor
  4. Using an FTP client to edit WordPress files manually

If you’re ready to get started, you can check out our collection of ten useful code snippets for WordPress sites. If you want to go even further, we have a guide on how to become a WordPress developer.

Do you have any questions about how to add PHP in WordPress? Let’s talk about them in the comments section below!

Free guide

4 Essential Steps to Speed Up
Your WordPress Website

Follow the simple steps in our 4-part mini series
and reduce your loading times by 50-80%. 🚀

Free Access

Inline Feedbacks
View all comments

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!