How to use PHP in WordPress posts, pages and widgets?
If you’re a PHP champ and want to add / execute (not embed) PHP in WordPress posts, pages and widgets (Text widgets) to achieve some extra functionality, this post is for you.
Using or executing PHP in WordPress posts, pages and widgets was never this easy before, as it is now with Exec-PHP plugin. Whether it is the task of limiting a widget on Home page only using PHP or to display a random ad, Exec-PHP is always there to achieve that with ease.
Features of Exec-PHP
- Can execute PHP code in the excerpt and the content portion of your posts and pages.
- Provides configurable execution of PHP code in text widgets (for WordPress 2.2 or higher).
- Allows using PHP code in familiar syntax, i.e. <?php … ?>
- Also works in the news feeds.
- Can be controlled for usability (for WordPress 2.1 or higher).
- Multi-language international support including English German and more.
While using this plugin, you may need to pay attention while making a post which includes PHP – you should switch to HTML editor instead of Visual (WYSIWYG) editor in WordPress. You can turn off the WYSIWYG warning by clicking on Users tab in the left pane, clicking Your Profile, checking Disable WYSIWYG Conversion Warning and click Update Profile
The only known issue with the Exec-PHP plugin for WordPress is that it doesn’t render the PHP code when a post is saved in draft as all the code is then converted to rich text while saving. But this can be bypassed by writing and directly publishing a post which contains PHP code.
You should read this document before start using this plugin. Sniplets is also a good alternative to Exec-PHP.
You may also like Display Source Code in Blog posts.