Beginner's Guide for WordPress / Start your WordPress Blog in minutes

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

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

11 CommentsLeave a Reply

  1. Does this plugin works with latest version of the wordpress? As this plugin is not updated for more than 3 years now.

    Also is it good terms of programming? As PHP functions get called inside another PHP functions ?

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

  3. 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.

  4. 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.

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

      • 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’)>”;

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.