WordPress GuidePHP → Run Script

How to add a PHP script on WordPress posts and pages

Image

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:

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:

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:

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:

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:

In your site’s wp-config.php file, you can enable debugging with:

php
CopyEdit
define(‘WP_DEBUG’, true);
define(‘WP_DEBUG_LOG’, true);

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:

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.

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