To display photos from the gallery section of this site in posts, I use PHP to call a function that grabs the details from a database. WordPress doesn’t natively allow you to do this due to the obvious potential security issues so I searched for a plugin to allow me to do it. I found Stevarino and also Pascal’s Hack, but I couldn’t get them to work (despite some help – big thanks Pascal!) so I just started from scratch with my own. Thanks to Stephen and Pascal for demonstrating how to apply filters to stuff in WordPress – I still have much to learn about the workings of WordPress. Spookily we came up with pretty damn similar code.
This is version 0.1, I can’t stress this enough 0.2 0.2.2, an early version – I developed it enough to get it working for my needs and then stopped. It hasn’t had any testing beyond that. In the winter, when I fancy using my computer more, I’ll try to improve it all. Not promising awesome support or anything, but feel free to leave comments and stuff as I do intend to maintain the code at some point. Obviously you should think about who has access to your blog and the code you’re going to be running when using this plugin.
Using WordPress 1.2, copy this code to a file named “runphp.php” and upload it to your “wp-content/plugins/” directory, then enable it in the admin panel. Use <phpcode>echo "hello mum!";</phpcode> to run some php.
<?php
/*
Plugin Name: Run PHP
Version: 0.2.2
Plugin URI: http://mark.scottishclimbs.com/archives/2004/07/02/running-php-in-wordpress-posts/
Description: Allows PHP to run in posts by using custom HTML style tags
Author: Mark Somerville
Author URI: http://mark.scottishclimbs.com/
*/
/*
run_php
This function runs and outputs PHP code that exists within <phpcode></phpcode> tags.
*/
function run_php($data) {
$tag = "phpcode";
$taglength = strlen($tag);
//html_entity_decode doesn't work! These lines change ' and " instead. Maybe other entities needed?
$data = str_replace(array("‘", "’"), "'",$data);
$data = str_replace(array("”", "“"), '"', $data);
$data = str_replace("″", '"', $data);
$data = str_replace("′", "'", $data);
while($phpstart = strpos($data, "<".$tag.">")) {
$phpend = strpos($data, "</".$tag.">");
$phptrueend = $phpend + $taglenth + 3 + $taglength;
$before = substr($data, 0, $phpstart);
$after = substr($data, $phptrueend);
$phpcodelength = $phpend - ($phpstart+2+$taglength);
$phpcode = substr($data, $phpstart+2+$taglength, $phpcodelength);
//remove the <br /> and <p></p> tags that WP adds to the code
$phpcode = str_replace('<br />', ' ', $phpcode);
$phpcode = str_replace('<p>', '', $phpcode);
$phpcode = str_replace('</p>', '', $phpcode);
ob_start();
eval($phpcode);
$data = $before.ob_get_clean().$after;
}
return $data;
}
add_filter('the_content', 'run_php');
?>
Changelog
- 0.1
- Initial release, scrappy.
- 0.2
- Fixed problem that meant PHP could only be on one line.
- 0.2.1
- Fixed an HTML entity problem with ″ (really need to get html_entity_decode working!)
- 0.2.2
- Fixed another HTML entity problem with ′ (still really need to get html_entity_decode working!)
Problems with Textile or Markdown?
Jeff suggests that you try changing the last line to:
add_filter('the_content', 'run_php', 5);
Dev note – I couldn’t get html_entity_decode to work at all and ended up just searching for single and double quotes. There are almost certainly some HTML entities that will screw things up. Has anyone had any luck with html_entity_decode?
Regarding html_entity_decode – a recent user contributed note (14/11/04) for html_entity_decode on the PHP documentation sheds a little light on the subject. The function doesn’t work with numeric encodings (which is the first type of error I encountered) and also doesn’t work with UTF-8 encoded text (which I use). I’m going to have a think about character sets and PHP versions to see what the best overall solution is.

How about of pages? Could you edit the code that it would work with pages and post.
How can i get the code not to skip a line?