◦ Comprehensive security
◦ 24/7 support
WordPress Guide → PHP → Run Script
How to add a PHP script on WordPress posts and pages
Out of the box, WordPress doesn’t let you run PHP code directly inside posts and pages. That’s by design—executing raw PHP in content can open the door to security risks and break your site if you’re not careful.
But there are safe and reliable ways to add PHP functionality to your content without touching your theme files or core code. Whether you want to display dynamic content, pull in custom data, or run a script you’ve written, you’ve got options.
Get fast, reliable hosting for WordPress
Power your site with the industry’s fastest, most optimized WordPress hosting
What is PHP?
PHP is a server-side scripting language used to create dynamic content. It runs behind the scenes every time someone loads your WordPress site. It powers everything from database queries to theme templates, plugin logic, and form submissions.
If you’ve edited a functions.php file, added a WordPress hook, or written a shortcode, you’ve already worked with PHP. But by default, WordPress limits PHP execution to templates and plugin files—not post or page content—so you need a workaround to safely execute PHP where it normally wouldn’t run.
How to add a PHP code manually
If you’re comfortable with a little coding, you can use a shortcode to execute PHP in a post or page. Here’s how:
1. Open your theme’s functions.php file or use a site-specific plugin.
2. Create a custom shortcode that runs your PHP code. For example:
function custom_php_shortcode() {
ob_start();
// Your PHP code goes here
echo “Today is ” . date(“l”);
return ob_get_clean();
}
add_shortcode(‘show_php’, ‘custom_php_shortcode’);
3. Use the shortcode in your content like this:
[show_php]
This lets you keep your logic separate from your post content and keeps the code contained in one place.
Warning: Don’t try to add raw PHP directly into the block or classic editor—it won’t work and may break your page.
Benefits of PHP code snippet plugins
For most users, using a plugin is the safest and most flexible way to run PHP in content. Plugins let you:
- Keep your custom code organized
- Control where and when snippets run
- Avoid editing theme files directly
- Turn snippets on or off with a toggle
- Prevent syntax errors from crashing your site
Two plugins that do this well: Insert PHP Code Snippet and WPCode.
How to add a PHP code with Insert PHP Code Snippet
Insert PHP Code Snippet is a purpose-built plugin that lets you create reusable PHP blocks and embed them anywhere.
To get started:
- Install and activate the plugin from the WordPress plugin directory.
- Go to XYZ PHP Code → PHPCode Snippets.
- Click “Add New PHP Code Snippet.”
- Name your snippet and paste your PHP code in the editor.
- Click “Create” to save it.
The plugin will generate a shortcode like [xyz-ips snippet=”snippet-name”] that you can insert into posts, pages, or widgets.
How to add a PHP code with WPCode
WPCode (formerly Insert Headers and Footers) is a more advanced option with built-in logic for targeting, scheduling, and conditionally loading snippets.
To use WPCode:
- Install and activate the plugin.
- Go to Code Snippets → Add Snippet.
- Choose “Add Your Custom Code (PHP)”.
- Give it a title, paste your PHP code, and choose “Auto Insert” or “Shortcode”.
- Save your snippet.
If you use the shortcode option, WPCode gives you a unique tag like [wpcode id=”123″] to drop into your content.
WPCode is ideal if you want more control over where the code runs (like only on specific post types, categories, or user roles).
Benefits of running PHP on WordPress
Adding PHP scripts to posts and pages gives you a ton of flexibility. You can:
- Display dynamic content like dates, weather, or stock prices
- Run custom queries or calculations inline
- Pull data from external APIs
- Add advanced interactivity that shortcodes alone can’t handle
- Avoid bloating your main theme files
With the right plugin and some safe coding practices, you can extend your WordPress site without turning it into a maintenance headache.
WordPress PHP FAQs
Where is the PHP error log in WordPress?
The PHP error log location depends on your hosting setup. Here are a few places to check:
- Inside your hosting control panel – many hosts (like cPanel-based ones) have a section for “Errors” or “Logs.”
In your site’s wp-config.php file, you can enable debugging with:
php
CopyEdit
define(‘WP_DEBUG’, true);
define(‘WP_DEBUG_LOG’, true);
- This creates a debug log at:
/wp-content/debug.log - If you’re on a VPS or dedicated server, check the server-wide PHP error log—often found at /var/log/php_error.log or something similar.
Where is functions.php in WordPress?
functions.php is located inside your active theme’s folder:
swift
CopyEdit
/wp-content/themes/your-theme/functions.php
This file runs every time WordPress loads and is often used to register custom code, filters, and shortcodes. If you’re using a child theme (which is recommended), edit the functions.php file in the child theme directory to keep your changes update-safe.
Which PHP version is best for WordPress?
WordPress recommends using PHP 8.1 or higher. As of now, PHP 8.2 is stable and widely supported, offering:
- Faster performance
- Better memory usage
- More modern language features
Avoid using PHP versions that have reached end-of-life (like 7.4), as they no longer receive security updates. Always check that your theme and plugins are compatible before upgrading.
Upgrade your WordPress experience
Ready to upgrade your WordPress experience? Professional hosting improves speeds, security, and reliability for a website and a brand that people find engaging and trustworthy. Liquid Web’s WordPress hosting options configure business-class servers and support plans specifically for WordPress websites.
Don’t want to deal with server management and maintenance? Our fully managed hosting for WordPress is the best in the industry. Our team are not only server IT experts, but WordPress hosting experts as well. Your server couldn’t be in better hands.
Click through below to explore all of our hosting for WordPress options, or chat with a WordPress expert right now to get answers and advice.
Additional resources
What is WordPress? →
A complete beginner’s guide—from use cases, to basics, to how to get started
PHP unit testing: How to write your first test for WordPress →
Ensure reliable WordPress development with unit testing in PHP, improving code quality and detecting bugs early.
How to embed iframe code in WordPress →
Benefits, limitations, and step by step guidance