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.
Then in your post, simply use the shortcode with the Snippet ID:
[ php function=ID]