Based on the work of Stevarino and to answer to WordPress Forum, here is another version :
I found the problem….It comes the formatting functions which convert few « special » characters into more fancy one….
function run_phpinpost($data) {
$PHPTag = "phpcode";
$data = str_replace(array("rn","r"), "n", $data);
while (false !== strpos($data, "< ".$PHPTag .">")) {
$Before= substr($data, 0, strpos($data, "< ".$PHPTag.">"));
$After = substr($data, strpos($data, ">")+(strlen($PHPTag)+3));
$Evaluate = substr($data, strpos($data, "< ".$PHPTag.">")+(strlen($PHPTag)+2));
$Evaluate = substr($Evaluate,0,strpos($Evaluate, ">")-1);
$Evaluate = str_replace(array("’","‘"),"'", $Evaluate);
$Evaluate = str_replace(array("”","“"),'"', $Evaluate);
ob_start(); // run that data and grab what comes out
eval($Evaluate);
$data = $Before.ob_get_clean().$After;
} // end of while
return $data;
} // end of run_phpinpost
add_filter('the_content', 'run_phpinpost');
>
to use it, simply put the code above in « my-hacks.php », and in your post use the tags :
<phpcode>php executable code </phpcode>
By the way if you are using an « old » PHP version, the function ob_get_clean() is not existing so :
// To add compatibility with earlier PHP version...(before 4.3.0)
if (!function_exists("ob_get_clean")) {
function ob_get_clean() {
$ob_contents = ob_get_contents();
ob_end_clean();
return $ob_contents;
}
}
I must admit that I like your improvements, it really is a much simpler method than my mess. 🙂 One critique I would make is to include error handling for the evaluated code. Look at the code
eval($var);
compared toif(!eval($var)) { ... }
.One of the biggest « problems » with this hack is people inputting faulty code such as forgetting a semi-colon. A little debugging assistance is sometimes greatly appreciated…But good job! If you don’t mind I’ll implement some of your improvements (with credit of course) next time I update my stuff.
Thanks ! 😉
You are right. An error handling is required ! And indeed usually the problem comes from the mising semi-colon…
But for the moment, I have the issue to avoid the sysematic texturized of posting even between < code> tags…. I know there is a way to « de-activate » that wonderful feature but….I can’t remember ! 🙁