Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Allow PHP in WordPress Posts and Pages

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Allow PHP in WordPress Posts and Pages

By default, you cannot execute PHP in WordPress posts and pages. We have made it a rule of thumb to never execute PHP in our posts and pages either because it can be hectic to change things in the future. A better practice would be to edit the WordPress theme by creating a custom page template, or single post template. However for some users, it may be necessary to execute PHP in WordPress posts. We couldn’t find a need for it, but apparently over 26,000 people have. So in this article, we will share how to allow PHP in WordPress posts and pages.

Simply install and activate the plugin Allow PHP in Posts and Pages. After installation, simply wrap your php queries inside the shortcode:

[ php] [ /php] – Please ignore the spaces. We cannot use this shortcode because it will pull our Syntax Highlighter Evolved plugin.

You can also save your php queries and call them on post or pages when you need them. To do this, simply click on Allow PHP in Posts menu option. Click on the Code Snippets Tab. Add your PHP query there.

Allow PHP in Posts and Pages

Then in your post, simply use the shortcode with the Snippet ID:

[ php function=ID]

Get Allow PHP in Posts and Pages

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. adam says:

    this is very old article hence given plugin is out of date ..

  2. Jorge Pecora says:

    This plugin works in WP 4.4.1 (January 2016)? or have a more updated to tell me suggestion. Thank you

  3. fastasleep says:

    var templateDir = “<php bloginfo(‘template_directory’)>”;

  4. LS says:

    does this work with wordpress version 3.9.1?

  5. griffith phelps says:

    This is good tutiral whihch expalin how wordpress allows PHP code to execte.

  6. Mohamed Tair says:


  7. Mark R Hughes says:

    No, for obvious reasons this would be an invitation to be hacked. Its very simple to create a shortcode instead (with your own plugin) or use something like the “post snippets” plugin instead.

  8. ilmv says:

    For the love of Christ, don’t execute PHP from the CMS, probably the single worst thing apart from publishing your FTP details as far as security goes.

    • Buk Lau says:

      really? but how the hell do you dynamically get the path of the theme folder? i need to get the to get this done.

      • fastasleep says:

        Put something like this in your template to create a javascript var to store the path, then use javascript to write it in your content?

        (removed some chars so this will show up –

        var templateDir = “<php bloginfo(‘template_directory’)>”;

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.