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]
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 ?
this is very old article hence given plugin is out of date ..
This plugin works in WP 4.4.1 (January 2016)? or have a more updated to tell me suggestion. Thank you
var templateDir = “<php bloginfo(‘template_directory’)>”;
does this work with wordpress version 3.9.1?
This is good tutiral whihch expalin how wordpress allows PHP code to execte.
thanks
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.
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’)>”;