Dec 15, 2025
Jordana A. & Ariffud M.
4min Read
Adding PHP to WordPress lets you add custom functionality, automation, and specific logic directly to your website’s architecture.
By adding these PHP snippets, you can customize theme behavior, extend plugin capabilities, and enhance security features beyond the default settings.
WordPress doesn’t allow direct PHP input in the block or classic editor because it would expose your site to code injection attacks – a vulnerability that could give attackers full server access.
To work around this safely, you can use two methods to add PHP to your WordPress site:
WordPress prevents users from adding custom PHP code directly in the built-in editor due to security concerns. To safely add PHP code to a WordPress post or page, you can convert it into a shortcode first.
You can create a shortcode in WordPress manually, but a plugin streamlines the process. Several options are available in the WordPress repository. For this tutorial, we’ll use one of the most popular choices: WPCode.
WPCode includes built-in error checking. If you make a syntax error or paste malformed code, it automatically deactivates the snippet and displays an error message explaining what went wrong.
Here’s how to add a PHP code snippet using the WPCode plugin:

// Display Los Angeles timezone
$timezone = new DateTimeZone('America/Los_Angeles');
$datetime = new DateTime('now', $timezone);
echo '<p>Current time in Los Angeles: ' . $datetime->format('g:i A') . '</p>';


If WPCode doesn’t meet your needs, consider these alternatives:
The plugin method makes adding custom PHP code easy, but it may be limiting for advanced customization. For more flexibility, you can add PHP manually.
Back up your WordPress website before changing any PHP files to protect your data in case something goes wrong. It’s also best to test changes in a staging environment instead of a live site unless it’s absolutely necessary.
There are two main ways to add PHP code to your WordPress site manually: using the WordPress theme editor or using a file manager or FTP client with a code editor.
The theme editor works well for quick edits because it’s accessible from your WordPress dashboard. But it lacks features like error checking and autocompletion that a proper code editor provides.
If you want to modify theme files, create a WordPress child theme and add your PHP snippets there. A child theme keeps your customizations separate and prevents theme updates from overwriting them.
If you’re using a modern block theme like Twenty Twenty-Four, some customizations that previously required functions.php can now be handled in the theme.json file. Check your theme’s documentation for the recommended approach.
Here’s how to add custom PHP in WordPress using the theme editor:
// Custom function to modify excerpt length
function custom_excerpt_length( $length ) {
return 30; // Number of words
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
To modify plugin files, go to Plugins → Plugin File Editor and follow the same steps. If you introduce a syntax error, WordPress deactivates the plugin and shows an error message so you can fix it.
On the other hand, editing files using your web host’s File Manager or an FTP client like FileZilla is better for users with some technical experience. This method also lets you access and modify files beyond themes and plugins.
Here’s how to add a PHP snippet using Hostinger’s File Manager:


For websites running on managed WordPress hosting, inserting custom PHP code offers the following benefits:
By adding PHP code to your WordPress core files, you can control how the server processes data and constructs pages before they reach the user. But a modern website needs more than server-side logic – it also needs interaction.
PHP handles the server-side work, while JavaScript runs in the browser to power dynamic features like live search, instant form validation, and interactive maps without requiring a page reload.
Combining PHP’s data processing with JavaScript’s interactivity is the standard approach for advanced plugin development and custom theme design. To round out your toolkit, read our guide on how to add JavaScript to WordPress.
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 ?