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.
Here’s how to add a PHP code snippet using the WPCode WordPress plugin:
- Install and activate the plugin. The Code Snippets menu will appear on the sidebar of your WordPress dashboard once the activation is successful.
- Navigate to Code Snippets → Add 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.
- 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.
- 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.
- 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.
- 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.
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:
- Head to Appearance → Theme file editor from your admin dashboard.
- Select Theme Functions on the right sidebar to open the functions.php file.
- 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 Plugins → Plugin 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:
- Select File Manager from your hPanel dashboard.
- Double-click on the public_html folder to browse your site files.
- 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.
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.
Suggested Reading to Expand Your WordPress Website’s Functionality
WordPress Tutorial
What Is HTML? Hypertext Markup Language Basics Explained
What Is CSS and How Does It Work?
What Is JavaScript? A Basic Introduction to JS for Beginners
Everything You Need to Know About wp-config.php
11 Best PHP Frameworks For Beginner to Pro Developers
WP-Content Uploads: What It Is and How to Upload Files in WordPress
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.
Conclusion
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.
Comments
May 16 2017
Thanks it was easy and great.....
May 28 2017
Thanks you very much i was searching for php code in wordpress like this
November 04 2017
I did exactly as you say but this plugin doesn't work properly, it is crap.
November 10 2017
Hey Devon, Why, do you get any errors? Provide more details, please.
December 19 2017
No errors, just doesn't work. No more details required.
November 08 2017
Is it possible that it doesn't work for 'pages'?
November 21 2017
I did exactly as you say but it doesn't work. It shows this message: Parse error: syntax error, unexpected ‘&’ in /home/nt435/public_html/wp-content/plugins/insert-php/insert_php.php(48) : eval()’d code on line 3
November 23 2017
Thank you Guys
April 17 2018
This plugin worked for me... had to deactivate some plugins before i got it to work
August 31 2018
Thanks a lot!
March 16 2020
You can consider: https://wordpress.org/plugins/ultimate-shortcodes-creator/. You can inject whatever code through a custom shortcode.
April 28 2020
Amazing content, thank you!
August 11 2020
Work for me
December 03 2020
Your simple example works, thank you. I am struggling with a more complex example: https://www.w3schools.com/php/php_form_validation.asp. How to get the form action to work. Any suggestions?
February 09 2021
Hi, Ian :) I'd suggest starting by enabling error logging and checking the error_log file - that should help detect the possible issue. If anything, I'd recommend considering using a plugin for your contact form :)
December 03 2020
The answer was to put the entire code block into the PHPSnippet
August 02 2022
Dear sir, I possible please tell me how i can display two prices on every product in the store, i need one price showing in euros(€) and the other one to be in croatian kunas (kn) Thank you kindly!
August 05 2022
Hey there! If you wish to display the price in 2 currencies, and you're using WooCommerce, you can use the filter woocommerce_get_price_html: add_filter('woocommerce_get_price_html', 'my_price_html, 99, 2); function my_price_html( $price, $product ){ return 'Was:' . str_replace( '', ' Now:', $price ); }. As an alternative, you can use plugins like CURCY And you can check this resource from WooCommerce to learn more about Multi-Currency settings.
February 16 2024
Sir can u please tell me i try to put some php code with wp code plugin..... and also with child theme... both work good in my site... Which method is good.. child theme or plugin please reply
February 26 2024
Hi there! Both methods, using a child theme or a plugin, have their advantages and depend on your specific needs and preferences. Ultimately, the choice between a child theme and a plugin depends on the nature of the changes you're making and your long-term maintenance strategy ?