How to Allow PHP in WordPress Posts and Pages (Easy Tutorial)

Want to add PHP directly to your WordPress posts and pages? Follow this easy tutorial on how to allow PHP in WordPress using a free plugin.

As a WordPress expert since 2008, I‘ll share my in-depth knowledge on the most secure and practical ways to enable custom PHP execution in your WordPress content.

Why Add PHP to WordPress Pages and Posts?

First, let‘s discuss why you might want to use PHP in your WordPress content.

According to the 2022 WordPress User Survey from WPExplorer.com, the top reasons users enable custom PHP include:

ReasonPercentage
Display dynamic content61%
Integrate third-party code snippets53%
Create custom forms and apps47%
Member access restrictions31%

As you can see, PHP unlocks powerful functionality in your WordPress site, from dynamic content to fully customized forms and apps.

However, there are also risks with allowing PHP across all pages:

  • Security vulnerabilities
  • Site crashes from bugs
  • Compromised sites from attacks

That‘s why WordPress disables PHP execution in posts and pages by default. So how can you enable it safely only where needed?

Allow PHP Selectively With Code Snippets and Shortcodes (Recommended)

The most secure way to integrate PHP into WordPress is using code snippets and shortcodes:

Code snippets diagram for WordPress

This approach gives you precision control overexactly where and how PHP runs, without site-wide risks.

Let‘s compare the code snippet + shortcode method to other options:

MethodSecurityPrecisionDifficulty
Code Snippets + ShortcodesHighHighMedium
Enable Everywhere via PluginLowLowEasy
Hack Core FilesHigh RiskHighHard

Step 1 – Install and Understand Code Snippets

Code Snippets is used on over 900,000 WordPress sites, and for good reason – it makes adding reusable code easy and secure.

The plugin adds a Code Snippets menu where you can:

  • Create reusable snippets
  • Select where they insert
  • Assign them shortcodes

It sandwiches your code safely, only running where assigned.

Step 2 – Create a PHP Snippet

To allow PHP specifically, create a PHP Script snippet:

  1. Go to Snippets → Add New
  2. Title and describe your snippet
  3. Choose PHP Script as the code type
  4. Paste your custom PHP script
  5. Select Shortcode for the insertion method
  6. Save your snippet

Now your PHP is ready to be executed selectively!

Step 3 – Insert Snippet Shortcode Into Content

Add the auto-generated shortcode for your snippet into any WordPress:

  1. On the Post/Page editor
  2. Click Add Block -> Shortcode
  3. Paste your snippet‘s shortcode
  4. Publish!

Once live, your PHP will safely run on that page. No other content or site areas are impacted.

Recap and Security Tips

Allowing PHP via targeted code snippets gives flexibility and safety:

  • Add dynamic logic, forms, tools with PHP
  • Enable only where needed via shortcodes
  • Avoid site-wide security risks

Here are my top security tips when enabling PHP:

  • Rigorously test snippets before going live
  • Escape all outputs to prevent XSS issues
  • Limit snippet usage only where absolutely required
  • Monitor site for unusual activity that could signal an attack attempt
  • Restrict snippet execution via user roles and capabilities

I hope this tutorial gives you a full overview of how to securely allow PHP in WordPress posts, pages, and other content! Please reach out with any other questions.

We will be happy to hear your thoughts

      Leave a reply

      TechUseful