How to Add PHP Code to WordPress Post or Page

By default, WordPress does not allow running PHP code in posts or pages. However, some of you might be needing this functionality. This tutorial shows you how to add PHP to WordPress posts and pages with a help of a plugin.

What you’ll need

Before you begin this guide you’ll need the following:

Step 1 — Installing Insert PHP plugin

PHP code can be run in WordPress posts and pages using Insert PHP plugin. Access your WordPress administrator dashboard and follow these steps in order to install this plugin:

  1. Under Plugins section press Add New button.
  2. Enter Insert PHP in the search field.
  3. Hit Install button.

Install Insert PHP

Refer to this tutorial on how to install and activate WordPress plugins if you need more detailed instructions.

Step 2 — Inserting PHP code in your post

Start a new WordPress post or page and put PHP code you want to run. Keep in mind that regular PHP opening and closing tags (<?php and ?>) must be substituted with short tags [insert_php] and [/insert_php] respectively:

Adding PHP ode to WordPress Post

In this example we will simply print out a date and time.

Step 3 — Previewing PHP code on your Post

Open up your newly created post and you will see that PHP code was executed:

PHP code in WordPress Post gets executed


Now you know how to add PHP code to your WordPress Posts and Pages. Running PHP code might be useful if you want to output local time, show contents of a file on your server, output a response from another URL and other tasks.

About the author

Gediminas B.

Gediminas was a passionate content writer and SEO geek at Hostinger. He started his journey at customer support and aims to use his gathered knowledge to create the most epic content the world has ever seen. From WordPress to server management, there's no topic he can't cover! He also likes cats.


Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!

More in WordPress
How to Clear WordPress Cache