How to Add PHP Code in WordPress Post or Page

How to Add PHP Code in WordPress Post or Page


As we know 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 or direvtly. By default, WordPress won’t let you execute any PHP code written in posts or pages. If you want to do something from there, one of the easiest ways is creating a shortcode in your theme’s functions.php file and calling that function in a post/page. You can also create a separate template file but if that’s not working for you and you really want to run the code directly from a post/page, you can still do that.

Simple method to add PHP Code in Worrdpress post or page

  • Navigate to Plugins->Add New
  • Search for “Insert PHP” plugin
  • Install and Activate it
  • Add new post/page or modify an existing one
  • Be sure to switch to Text Editor because Visual one won’t work for you in this case
  • Write your PHP code:

This is just one simple trick that can help you with your WordPress blog on an everyday basis.

Below the whole process to add PHP code in WordPress post or pages with snapshots so you can easily understand and implement as well.

 Step 1 —PHP plugin Insert and Installing

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.

Step 2 —  PHP code Inserting in post

Start a new WordPress post or page and put PHP code you want to run.

Important thing that regular PHP opening and closing tags are (<?php and ?>) respectively must be

substituted with short tags [insert_php] and [/insert_php] :

For PHP Editing its needed to use  opening and closing tags [insert_php] and [/insert_php] respectively.

For Example : Enter the below code in your page/post to apply simply print out a date and time.

echo 'Current date and time is: ';
$timezone = +5; //(GMT +5:30) EST (India)
echo gmdate("Y/m/j H:i:s", time() + 3600*($timezone+date("I")));




Step 3 — Previewing PHP code on your Post

Open up your newly created post and you will see that PHP code was 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. That’s all there is. Now you can easily run any PHP code from posts and pages directly and all you have to do is to replace PHP tags with the shortcode. Still, be careful because the plugin can’t do magic – all coding errors will be executed as well. If you need more information about the plugin, please visit the official pages. If you want to run the PHP code from Text Widget, you can do that without the need for a plugin.

Add a Comment

Your email address will not be published. Required fields are marked *