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

How to Allow PHP in WordPress Posts and Pages

Last updated on by
Follow WPBeginner on YouTube
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 lovers 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 »
  • griffith phelps

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

  • http://www.r-gate.net/ Mohamed Tair

    thanks

  • Mark R Hughes

    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.

  • ilmv

    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

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