Mar 02, 2026
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.