I have just written a plugin which allows you to run PHP code from a Wordpress post or page. The story is that I needed to allow a Wordpress user to hook into a bespoke function from the body of a page. There were two obvious ways to do this:
- Add a new Wordpress Page custom template and put your code in there (no security holes but severely limited in its uses)
- Add a new plugin to allow PHP code to be embedded into post content
Obviously I have gone with the plugin method for its increased flexibility and portability but I must mention before we go any further that it makes use of the PHP exec command. As you may well know this allows the user two run ANY code they like and this plugin merely facilitates that by adding it to Wordpress. There are, however, so many more plus points for this type of ability so it’s an acceptable risk (to me
).
The plugin uses the Wordpress shortcode API (so no horrible regular expressions) to add a new hook into the content of a post or page. Any content encapsulated in the hook is considered to be PHP code and will be executed every time that page or post is shown. The plugin can easily handle smart quotes and HTML so there is no need to use the HTML tab within the post edit page like some other plugins require you to do.
Usage
To use the plugin you simply use [sb_php][/sb_php] and put PHP code between the hooks as follows:
[sb_php]echo 'this is PHP code';[/sb_php]
will output:
this is PHP code
Download: SB Post PHP (408 bytes)
