(Note: PHPExec2 is no longer being updated – see this post for details).
Sometimes, adding PHP directly to your blog entries can be a cleaner and simpler solution than other alternatives. Although you can edit the WordPress template directly to display, it can get messy, especially when all you want to do is create a single page with a little extra ‘smarts’ in it.
In reviewing the different solutions out there, I soon settled on PHPexec as it was very easy to set up. Just place the single phpexec.php file in the plugins directory, activate it, and include the special tags around the PHP code you want to execute. The only setting (Under Admin’s Options;PHPExec tabs) is to set the security level – by default it’s at 9, so anyone posting with a user level of 8 or less cannot enter PHP code – simple security for blogs with multiple posters (if you’re the only poster, you needn’t touch this setting).
However, one problem I noticed is that the plugin only works in WP version 1.5 – in version 2 the code doesn’t display properly. So I dug into the code, fixed it, and notified the author about the changes.
Unfortunately, there’s been no response, so I’m posting the fixed code on my site at ActiveBlogging.com. This code will work the same in WP 1.5 and 2 so upgrading won’t be a chore if you have an older blog you’ve been ‘holding out on’ because of this plugin’s bug.
Once set up and running, adding code to pages and posts is very simple – just enter your PHP in the post or page with surrounding tags like this:
<phpcode>
<?php echo date("F j, Y, g:i a"); ?>
</phpcode>
with these tags, the code will be executed rather than displayed – in this case, you’ll see the current date and time displayed (for the server of course, which will likely differ from your local time).
One warning about the version 2 modification – you CANNOT edit the code in the graphical HTML editor . So to enter and edit code, you’ll need to turn off the graphical editor and go back to ‘classic’ mode.
In Admin, go to Users;Profiles; then ‘Personal Options’ at the bottom, and uncheck the ‘Use the visual rich editor when writing’ option, and update the page. Following that, you’ll be able to edit the code properly (you can always turn it back on once you’ve finished).
The PHP plugin stands on its own as a useful WordPress plugin tool, and can be used for anything. Anywhere you want PHP code executed in a WordPress post or page, this can be a real time saver!
(download from here: http://activeblogging.com/misc/phpexec.zip)