WordPress Tutorial

How to Add PHP Code to WordPress Post or Page

Introduction

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.

[optin-monster-shortcode id="cgx87nzwxn0himxw62bn"]

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

Conclusion

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.

7 Comments

Click here to post a comment

Join thousands of subscribers worldwide

and get awesome deals & the newest tutorials to your email

Please wait...

Thank you for sign up!

More in WordPress Tutorial
How to Clear WordPress Cache

Close