Adding PHP to WordPress: 2 Methods to Add PHP Code Snippets

Adding PHP to WordPress: 2 Methods to Add PHP Code Snippets

WordPress is an open-source content management system (CMS) built with PHP, a popular scripting language used by 76.8% of websites. Knowing how the scripting language works can help you get more from your website.

The problem is that WordPress doesn’t allow users to add PHP code through the block or classic editor for security reasons. You’ll need to either use a plugin or add it manually to your website’s files.

This article will explore different ways of inserting PHP code into WordPress. The steps are easy to follow, even if you’re not a WordPress developer.

Download All in One WordPress Cheat Sheet

2 Methods for Adding PHP to WordPress

Before diving into the tutorial, it’s a good idea to understand PHP and its role in WordPress. Once you’re comfortable with that, explore the following methods for adding custom PHP code based on your specific needs and situation.

Important! Back up your entire WordPress website before adding code snippets to keep your data safe.

How to Add PHP Code Using a Plugin

As mentioned earlier, WordPress restricts users from adding custom code directly via the built-in editor to prevent code injection attacks. To safely insert PHP code into a WordPress post or page, you can convert it into a shortcode first.

While you can manually create a shortcode in WordPress, a plugin can streamline the process. Several plugins for adding custom code are available in the WordPress repository. For this tutorial, we’ll use one of the most popular options ‒ WPCode.

WPCode plugin banner.

Here’s how to add a PHP code snippet using the WPCode WordPress plugin:

  1. Install and activate the plugin. The Code Snippets menu will appear on the sidebar of your WordPress dashboard once the activation is successful.
WPCode plugin listing in the WordPress directory.
  1. Navigate to Code SnippetsAdd Snippet to find a library of pre-made code snippets for specific functions. Since we want to add custom PHP code, select the Use snippet button under the Add Your Custom Code (New Snippet) option.
Creating a new PHP code snippet with WPCode. The "Use snippet" button under the "Add Your Custom Code" option is highlighted.
  1. Insert a name for your new PHP code snippet and write the code in the Code Preview box. Don’t forget to set the Code Type dropdown field to PHP Snippet. For this example, we create a custom code snippet to retrieve Los Angeles’ time zone and name it “Get Time Zone.” Hit Save Snippet.
Adding new PHP code to the Code Preview section in WPCode.
  1. The Insertion section determines how you want to add the new PHP snippet to your website. Let’s try using Auto Insert and setting the location to Site Wide Footer. Select the toggle in the upper menu bar to activate the code snippet and hit Update. This setting will display the requested time zone in the footer section of all pages.
Displaying the "Get Time Zone" function across the footer of all pages.
  1. The Shortcode option turns your code into a simplified version that you can copy and paste on WordPress posts and pages. Hit the Copy button and paste the shortcode on your chosen WordPress post or page.
Turning a code snippet into shortcode with WPCode.
  1. If you choose the Auto Insert option, you can set a conditional logic to execute the custom PHP code. The free version offers User and Page parameters, which include the user role, page type, and specific URL. Select Add new group and configure the conditional logic, then hit Update.
Setting up a conditional logic for a code snippet with WPCode.

If WPCode doesn’t meet your needs, consider other WordPress plugin options below:

  • Code Snippets ‒ includes a code editor with error-checking features and lets you group similar snippets using tags.
  • Head and Footer Code ‒ sets the priority of custom code on the header, body, and footer sections.
  • Post Snippets ‒ changes the order of snippets using drag-and-drop functionality.

Pro Tip

The WPCode plugin also lets you insert custom code in other programming language. For example, check out our article to learn more how to use it to add JavaScript to WordPress websites.

How to Add PHP to WordPress Manually

The plugin method makes inserting custom PHP code to your site easy, but it may be limiting for advanced customization. For greater flexibility, consider doing it manually. Remember to back up your website before making changes to any of the PHP files to keep your data safe in case of errors.

There are two main ways of adding PHP code to your WordPress site manually ‒ using the WordPress theme editor and a File Manager or an FTP client with a text editor.

The theme editor is ideal for adding PHP code quickly, as it’s accessible from your WordPress dashboard. However, it lacks code writing capabilities that code editors offer, such as error-checking and auto-completing.

Here’s how to add custom PHP in WordPress using the theme editor:

  1. Head to Appearance Theme file editor from your admin dashboard.
  2. Select Theme Functions on the right sidebar to open the functions.php file.
Functions.php file as seen in the WordPress theme file editor.
  1. Add the relevant code for your desired functionality to the WordPress editor, then hit Update File.

If you want to modify the plugin files, navigate to PluginsPlugin file editor and follow the same steps as above. Keep in mind that the plugin and theme editors’ location can vary depending on your active theme.

On the other hand, making changes to your WordPress files using your web host’s File Manager or an FTP client like FileZilla may be best for users with some technical knowledge. This method also lets you access and modify other files beyond themes and plugins.

Here’s how to add a PHP code snippet to WordPress using Hostinger’s File Manager:

  1. Select File Manager from your hPanel dashboard.
The File Manager in Hostinger hPanel is highlighted.
  1. Double-click on the public_html folder to browse your site files.
The public_html directory in File Manager is highlighted.
  1. Enter the wp-content folder to make changes to your themes and plugins. For example, open themes theme folder and insert PHP functions to the functions.php file. Double-click on the PHP file to open it, and hit Save once you’re done.
The TwentyTwentyOne theme's directory with the functions.php file highlighted.

If you want to modify your theme files, we recommend creating a WordPress child theme and adding your PHP code snippets there. Keeping the child theme separate from its parent lets you maintain the original code and ensure that theme updates won’t overwrite your customizations.

Why Add PHP to WordPress

For websites running on managed WordPress hosting, inserting custom PHP code offers the following benefits:

  • Enhanced functionality ‒ add unique features that might not be achievable with standard plugins or themes.
  • Tailored solutions ‒ create customized solutions for your specific requirements and growing user demands.
  • Unique designs ‒ implement special design elements and layouts that set your website apart from others using off-the-shelf themes.
  • Integration ‒ integrate external services, APIs, or databases seamlessly into your WordPress site, enabling complex interactions and data handling.
  • Automation ‒ automate repetitive tasks to save time and streamline data processing.
  • Improved security ‒ if implemented correctly, you can gain better control over your website’s security measures.
  • Cost savings ‒ you don’t have to rely on expensive third-party plugins to scale your website.


WordPress, powered by the widely-used PHP scripting language, offers incredible potential for website customization and expanded functionality. Understanding PHP’s role in WordPress and how it works can empower you to maximize your website’s capabilities.

Let’s recap two effective ways to add new PHP code to your WordPress site:

  • Using a WordPress code snippet plugin, such as WPCode.
  • Adding code manually via the WordPress theme editor, your web host’s File Manager, or an FTP client.

We hope this article helped you tailor your website using PHP code in WordPress. If you still have questions, consult our FAQ section or post them in the comments section below.

Adding PHP to WordPress FAQ

This section covers some of the most frequently asked questions about inserting PHP in WordPress sites.

How Do I Add PHP to My WordPress Site?

You can either use a WordPress plugin or do it manually via the WordPress theme editor, your web host’s File Manager, or an FTP client.

Can I Insert PHP Code Into a WordPress Page Without a Plugin?

Yes, you can. Define the PHP snippet in one of your website files and paste the shortcode anywhere on the WordPress page.

Where Do I Put the PHP Code?

The location of your code depends on its purpose. If you’re working with code snippets that modify plugins, themes, or website content, they should be placed in the wp-content directory. If you want to change your site’s functions, add the PHP code to the wp-config.php file. Regardless of where you put the code, remember to back up your website before making any changes to safeguard your data in case of errors.

The author

Jordana Alexandrea

Jordana is a Senior Content Writer with over 5 years of experience in digital marketing and web development. When she’s not busy with work, she dabbles in creative writing and movie reviewing. Follow her on LinkedIn.