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:
Table of Contents
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:
- Under Plugins section press Add New button.
- Enter Insert PHP in the search field.
- Hit Install button.
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 (
?>) must be substituted with short tags
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:
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.