How to Add PHP Code to WordPress Post or Page
access_time
hourglass_empty
person_outline

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

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.

The Author

Author

Gediminas B. / @gedziuzzz

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.

Related tutorials

Author

okereke Reply

May 16, 2017

Thanks it was easy and great.....

Author

Naqi Reply

May 28, 2017

Thanks you very much i was searching for php code in wordpress like this

Author

Devon Reply

November 04, 2017

I did exactly as you say but this plugin doesn't work properly, it is crap.

    Author

    Domantas G.

    Replied on November 10, 2017

    Hey Devon, Why, do you get any errors? Provide more details, please.

      Author

      Eduardo

      Replied on December 19, 2017

      No errors, just doesn't work. No more details required.

Author

Dario Reply

November 08, 2017

Is it possible that it doesn't work for 'pages'?

    Author

    Alex

    Replied on November 21, 2017

    I did exactly as you say but it doesn't work. It shows this message: Parse error: syntax error, unexpected ‘&’ in /home/nt435/public_html/wp-content/plugins/insert-php/insert_php.php(48) : eval()’d code on line 3

Author

Fernando Reply

November 23, 2017

Thank you Guys

Author

bv nityanand reddy Reply

April 17, 2018

This plugin worked for me... had to deactivate some plugins before i got it to work

Author

Robson Reply

August 31, 2018

Thanks a lot!

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

Become a part of Hostinger now!

More in WordPress
How to Clear WordPress Cache

Close