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.

&lt;?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("&#8216;", "&#8217;"), "'",$data); $data = str_replace(array("&#8221;", "&#8220;"), '"', $data); $data = str_replace("&Prime;", '"', $data); $data = str_replace("&prime;", "'", $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'); ?&gt;

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 &Prime; (really need to get html_entity_decode working!)
0.2.2
Fixed another HTML entity problem with &prime; (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.

102 Responses to “Running PHP in Wordpress posts”

  1. Stephen Says:

    Cool, I like what you’ve done. I’ve been meaning to convert the code to a plugin forever (this is one of my faves) but havn’t had the time.

    html_entity_decode is available in php 4.30 and above. I’ve found that a large number of wp users have older installs, so I try to keep it at about 4 and lower.

    And out of curiosity, why do you call it “Pascal’s hack?”

  2. Mark Says:

    Hi Stephen. Think I’ve sussed out what’s happened!

    A guy called Pascal developed his own hack based on your code (I think). My small brain got confused and thought that both bits of code were his :o. Oops, sorry.

    I’ve changed the post so it hopefully makes more sense! Nice one on getting the hack going in the first place.

    I can’t remember what was happening with html_entity_decode – only that I was getting some strange results. I hadn’t thought of folk with older versions though, so maybe it’s worth leaving it as it is.

  3. Yablog Says:
    <trackback />More Weather WeatherMoreIcon(‘EGPH’,’Edinburgh,Scotland’); WeatherMoreIcon(‘EGLC’,’London,England’); WeatherMoreIcon(‘KNYC’,’New York,New York’);

    Updated and cached every 30 minutes. Locations: Edinburgh Airport, London City Airport and…

  4. dodo Says:

    hi i tried install this plugin but i got

    Parse error: parse error in /home/pure/public_html/wp-content/plugins/run_php.php(33) : eval()’d code on line 2

    any idea?

  5. Mark Says:

    Hi there. There was a problem with spreading PHP code over more than one line. It was something I was going to fix but never got round to…

    I’ve changed the code a bit, and it should be working now. Thanks for the kickstart! :)

  6. Josh Says:

    After enabling this plugin, whenever I edit a post, I get:

    Warning: Cannot modify header information – headers already sent by (output started at …/wordpress/wp-content/plugins/runphp.php:45) in …/wordpress/wp-admin/post.php on line 340

    Yet the post seems to save, and shows up correctly on the webpage.

    Ideas?

  7. Josh Says:

    I get that same error when logging out too.

  8. Mark Says:

    Hmm, that’s pretty strange – something is trying to send headers from post.php. Has this been modified in any way? Perhaps another plugin is conflicting?

  9. Shimirel Says:

    I’ve managed to get the Warning: Cannot modify header information – headers already sent by (output started at …/wordpress/wp-content/plugins/runphp.php:45) in …/wordpress/wp-admin/post.php on line 340 error. By trying to call a function in another plugin. Say calling the wp_ozh_getCountryName(); function in the wp_ozg plugin.

    Its a great addon though many thanks, i’m sure its going to be useful.

    Regards, Shimirel

  10. Mark Says:

    Thanks Shimirel, that could help a lot when I get round to looking at the plugin again (hopefully soon). Hope you find it useful somehow.

  11. Josh Says:

    Mark, I used your exact code. I was running the Textile 2 plugin as well.

    However, I’ve left WordPress for this project, so I don’t need it anymore. Thanks for your work, though.

  12. TechGnome Says:

    To those getting the “Cannot send headers….” error. Make sure there is no whitespace after the closing ?>. This a common issue, and 99% of the time happens when copying from a webpage is involved. If you put your cursor at the end of the file, it should be immediatly after the closing ?>. If there is a space, or the cursor is on the next line below, then hit the backspace a few times.

    TG

  13. Shimirel Says:

    Yeah your spot on TechGnome I just came back to post a message saying the exact same thing. I found whitespaces at the end of Marks script and the one I was trying to call. Removed them and now I’ve got my random spell script to work yay ;)

  14. TechGnome's World &raquo; Introducing StaticEez Says:
    <pingback />[...] will be used for the title of the link. Now, here’s a bonus feature. If you also get the run php plugin you can also then put phpcode into the post. Th [...]
  15. clkung Says:

    Hello, Mark I have installed the Run PHP plugin and found an error in my post like this:

    Parse error: parse error, unexpected ‘< ’ in /home/kkk/public_html/wordpress/wp-content/plugins/run-php.php(39) : eval()’d code on line 1

    I noticed that wordpress has changed following line in my post from: <phpcode>< ? phone_list() ?> to: <phpcode>< ? phone_list() ?></></phpcode>

    It seem that wordpress has added the </> and was not removed by the RunPhp code. Can you suggest how I can solve this problem ? Thank you

  16. Mark Says:

    Although it doesn’t show up right on your comment, I got the code you posted in my e-mail alert.

    I think the problem is that you have added PHP open and close tags within the [phpcode] tags. You don’t need to do this as the [phpcode] tags replace the open and close tags. Your code should run using something like:

    [phpcode] phonelist(); [/phpcode]

    :)

  17. clkung Says:

    Mark Thanks for the pointer, I have corrected that, but I still get an error:

    Parse error: parse error, unexpected $ in /home/kcl/public_html/kkk/wp-content/plugins/run-php.php(38) : eval()’d code on line 1

    The line 38 is : eval($phpcode); I do not see a $ in this line. Can you help ?

    Thank you.

  18. Mark Says:

    eval($phpcode); is the line that actually runs (evaluates) the PHP code, so any parse errors that you get there are a result of an error in the PHP between your [phpcode] tags. I’ve no doubt that there are a few wee problems with the plugin script that will sometimes mean you have parse errors in what looks like valid code though!

    However, let’s just get some very simple PHP going, to make sure it all works as it should. After that you should be able to add in your code.

    '[p]RunPHP is a buggy pile of poo![/p]';[/phpcode]

    is about the simplest I can think of. Change the square brackets to pointey ones and let me know how it goes.

  19. Jacob Says:

    So I was messing around with your nifty script. At first it worked great, but I ran in to a bug of sorts. For some reason or another when I pass a variable that ends with a number things get weird.

    like try <phpcode> callthisfunction(“sometext1”) </phpcode> When it tries to eval the close quotes have a funky ” mark and in html it says callthisfunction(“sometext1″);

    What is going on?!

  20. Jacob Says:

    When I posted, it seems to have converted a bit of what I typed…

    Like callthisfunction( ” s o m e t e x t 1 & P r i m e ; ) ; added spaces…hopefully it will print…

    Parse error: parse error in wp-content/plugins/runphp.php(38) : eval()’d code on line 1

    Email me if you want the exact output.

  21. Mark Says:

    Jacob – this stems from my problem with getting html_entity_decode to work properly. Until I get off my arse and try to fix the problem properly, I suggest you use the new code that I have posted. Let’s call it version 0.2.1.

    Let me know if that doesn’t work.

  22. Colin Lewis Says:

    I’m a Wordpress newbie who has happily found your plugin.I’ve run the test code as stated in comment no. 18 ([phpcode]echo ‘[p]RunPHP is a buggy pile of poo![/p]’;[/phpcode]) and it worked perfectly. My other attempts result in a parse error:

    “Parse error: parse error in …/plugins/runphp.php(39) : eval()’d code on line 1”

    I’m trying to run the following php within my post (substituted square for pointy brackets here):

    Any thoughts on how this can be fixed? I’ve tried

  23. Colin Lewis Says:

    oops, hit the submit button ahead of schedule there. I meant to say ” I’ve tried both with and without the extra php brackets.”

    Cheers, Colin

  24. Colin Lewis Says:

    OK – the solution was easy – only had to identify the problem, which was spelled “HUMAN ERROR.” One little extra bracket that fatigue rendered invisible.

    Thanks a million for this code. Without it, I really couldn’t have tweaked Wordpress to do what I need it to do.

    best regards, Colin

  25. Mark Says:

    Thanks, glad you’ve got it sorted and find it useful.

  26. Blair Says:

    Hi ya Mark

    Just loaded this nifty plugin onto 1.2 and it’s working a treat so far…only loaded the date but even thats better than nothing eh :0)

    I’m sure it will keep this :newbie: entertained for hours. Added to my ever growing list of plugins.

    Its people like yourself who make wordpress so neat. Thanks a bunch.

  27. h0bbel Says:

    Thanks! This helped me build by stats page, worked just perfectly!

  28. Emily from discount toys Says:

    Hi Mark -

    Great plugin! It’s allowed me to FINALLY accomplish what I wanted to do with my discount tools and hardware blog!!!!!

    THANK YOU! When I have time, I’ll also update my blogging tips site to link to your code! It’s a MUST-HAVE!

  29. Emily from discount tools Says:

    by the way, the site I’m using your code on is my wordpress installation at http://www.sealectric.com/tools/

    It allows me to run code from w/in my WordPress post so that the posts themselves are actually dynamic! I’m so excited. Thank you again SO MUCH for this plugin!

  30. How To Blog Says:
    <trackback />Wordpress plugin – Running PHP in Wordpress posts Mark Somerville wrote a REALLY cool Wordpress plug-in that allows you to include PHP code in your wordpress posts, which really expands the potential of what you can do with Wordpress.
  31. ryan Says:

    The script doesn’t seem to be able to handle strings containgin numbers. If I do echo ‘test’, there’s no problem. If I do echo ‘234’ or echo ‘test2’, I get this error: Parse error: parse error, unexpected $ in /home/ryan4321/public_html/webcommunicate/projects/wp-content/plugins/runphp.php(39) : eval()’d code on line 1

  32. ryan Says:

    re: my post above, I’m running WP 1.2.1 and runphp 0.2.1.

  33. Mark Says:

    Hi Ryan.

    Hopefully this new version has sorted that problem out. :)

  34. ryan Says:

    wow. very nice! it’s working great on my site. I’m using it to integrate a screenshots database with my blog. My screenshots database builds the PHP code for the screenshot, w/ url, caption and dimensions for thumbnail and large image, and I drop something like the following into my blog entry:

    <phpcode>require (‘http://www.mysite.com/screenshots/insert.php?id=31’);</phpcode>
  35. ryan Says:

    well, just when I thought it was working… and it was indeed working for a few days, but now I get this error on my page:

    Warning: Unknown(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /path/on/my/server/to/runphp.php(40) : eval()’d code on line 1

    Warning: Unknown(http://www.webcommunicate.net/screenshots_pub/insert.php?id=34): failed to open stream: Success in /path/on/my/server/to/runphp.php(40) : eval()’d code on line 1

    Fatal error: (null)(): Failed opening required ‘http://www.webcommunicate.net/screenshots_pub/insert.php?id=34’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /path/on/my/server/to/runphp.php(40) : eval()’d code on line 1

    FYI this happened just after I added a new category in WP, then assigned an existing entry to that category, then tried to navigate to the entry through the new category link… that’s when the above error occurred.

  36. ryan Says:

    update: The above mentioned error also occurs when I use my browser back arrow to return to a page that has a WP entry that uses runphp.php. It’s the same error message as above. This all appears to be intermittent - sometimes the pages function perfectly well - which is perhaps more worrisome than if it happened every time. Also, sometimes my pages seem to take a long to open when I use runphp.php, but it’s hard to say whether that’s the fault of runphp or something else.

  37. ryan Says:

    are you aware that there’s a plugin by the same name that purports to do the same thing: http://www.nosq.com/technology/2004/10/runphp-wordpress-plugin/

  38. Mark Says:

    That’s a pretty funky error, Ryan! I’m not sure what is causing it. I also cannot reproduce any long load times. As you say, perhaps the strangest thing is that the problem only occurs intermittently. Knowing that it was working for a few days at least tells us that something somewhere must have changed, but I’ve no idea what. Let us know how you get on with the problem.

    I wasn’t aware of that other plugin, thanks for pointing it out.

    Good luck!

  39. paul haine Says:

    I’ve noticed a small problem with this plugin; it’s interfering with the wptexturize function (wp-includes/functions-formatting.php) which converts single quotes and double quotes into the typographically-correct curly equivalent. When this plugin is activated, those quotes aren’t converted anymore, and this is true for every single post, not simply those that are using <phpcode> within them.

  40. Mat Says:

    top plugin! :)

  41. justin Says:
    fyi, the function ob_get_clean didn’t show up until php v4.3, so if you want to extend the backwards compatibility of your plugin, just replace the last two lines inside the while loop with: $data = $before . ob_get_contents() . $after; ob_end_clean();
  42. Mark Says:

    Thanks Justin. I’ll incorporate that once I get round to re-coding it.

    Keep the bugs coming… :)

  43. Dave's Chalkboard &raquo; Mark&#8217;s blog >> Running PHP in Wordpress posts Says:
    <pingback />[...] Filed under: PHP programming Plug-Ins WordPress — VesperDEM @ 9:19:07 am Mark’s blog >> Running PHP in Wordpress posts Hopefully [...]
  44. Elliott Back Says:

    Awesome. Works like a charm.

  45. roger Says:

    This plugin can do some amazing things however I had to deactivate because it brought down my whole isp server!

    I was using it for a simple “include” for bringing in parsed rss feeds.

    The results were stunning. However somehow in the process I had a gig of error logs in two days! This in turn shut my site and since it was in my root, it also shut the whole server! (and my site is not that busy!!!)

    I’ve dropped back to using iframe for the parsed rss. It looks crappy but I haven’t had any more problems.

  46. Jeff Says:

    Wonderful plugin. Other than problems with headers due to spaces, it works well. Also, the last line, when you add the function to the content, you should add another argument. It should read like this:

    add_filter('the_content', 'run_php', 5);

    instead, so that it is not already processed by Markdown or Textile, messing up the quotes.

  47. Davin Says:

    Does this plugin work OK with 1.5 (Jan 31st)?

  48. ODB Says:

    Very cool, I was looking to run PHP in my Wordpress posts. Basically using it to populate a photo gallery. My php function simply reads a DIR I pass and then displays the photos. So in my WP post I’m able to call the function and pass the DIR to read now. Thanks!!!!!

    One thing. When I look at my WP plug-ins admin - There’s a bunch of text for this plugin. The others have 1 or 2 lines of text, but this one has a bunch of text expanding the table really big.

    Is that normal? Thx.

  49. Mark Says:

    Roger – funky error, sorry!

    Jeff – that line did some funny stuff to my PHP output – I’ve added it as a note.

    Davin – no idea!

    ODB – glad it’s working well for you, but that doesn’t sound too right. The text in that table should just come from the first few lines of the plugin code. Maybe try messing with them to see what happens.

  50. Davin Says:

    The answer is no – it does not work with 1.5. It gives some sort of header error – probably not a big deal to fix, though.

  51. david clark Says:

    Hi,

    I successfully got this to work in 1.5.

    There are two things to jook out for: 1. make sure you have a ; 2. the action nehds the have a high numbe, as in add_filter(‘the_content’, ‘run_php’, 1);

  52. Jess Says:

    Hi, thanks for the code? Will it work in Custom Fields? I tried adding a filter but that doesn’t seem to work…

  53. Mark Says:

    Jess, I really don’t know. Sorry!

    If you get a chance, let us know what you find out though.

  54. Enrique Espinosa Says:

    How could I get this to php in post thing in Wordpress 1.5? Need to run php within posts.

  55. Señor Epiphyte Says:

    The first time I followed the directions of the script author which resulted in a plugin that I could activate, but then failed at line 17. I read over the website several times, uninstalled the plug and started over.

      Steps I took toward solving this problem:
    • Followed the author’s directions, again
    • Used this alternative, but I changed the third parameter to a 1 for my system…dunno why, just decided to do it after reading his website.
    • original code in the author’s file ‘run_php’);[/php] updated to ‘run_php’, 5);[/php] looks like after ‘run_php’, 1);[/php]
    • Realized none of these worked and so I hunted in recent memory for a solution. I remembered many problems pasting text and code from websites. I removed all stray white spaces and replaced all line breaks with fresh returns in Dreamweaver.
    • On uploading the file after this revision to the interstitial spacing—success! Success in that it did not crash wordpress and I was able to activate the plugin.
    • Tested with the simple [code]echo ‘runphp is poop’;[code] and it was a success.

    Now it works nicely and I can show you things that exist in php function calls like this one:

    Radical!

  56. Zonekiller Says:

    Hi I’m a newbie :( and I’m trying to use this plugin to insert my blog archive in a brand new page (wordpress 1.5).

    This code:

    <phpcode>< ?php wp_get_archives(‘type=monthly&format=other&after=

    ‘); ?></></phpcode>

    get me the following error:

    Parse error: parse error, unexpected ‘<’ in /membri/videodrome/wp-content/plugins/runphp.php(40) : eval()’d code on line 1

    What should I do? Please help!

  57. Zonekiller Says:

    Ops, the post I wrote doesn’t work properly… anyway I put the code “WP GET ARCHIVES…” between <phpcode> and </phpcode> but I get an error:

    “Parse error: parse error, unexpected ‘<’ wp-content/plugins/runphp.php(40) : eval()’d code on line 1”

  58. Zonekiller Says:

    Sorry I don’t know how to post my code here in the right way…

  59. Señor Epiphyte Says:

    ZoneKiller, I had the same problem with the extra ’ until I screwed around with the code as above. Also don’t forget to change your [ braces ] with < symbols >.

  60. Señor Epiphyte Says:

    These guys &#60 and &#62 need to be used but don’t show up on the comment page…

  61. Simple Thoughts Says:
    <trackback />How to post dynamic content ( using php ) on WordPress Blogs + Know your IP Address It is a very strong capability to be able to post dynamic content in your blog. You can use this to integrate contents from other websites, fetch data from your database etc. It enables whole plethora of functionalities to your blog posting to make it…
  62. Soma Says:

    if you add:

    global $allowphp if($allowphp != 3) { // or some other weird number/phrase return; }

    at the beginning of your plugin, and then in your index.php (and/or other applicable pages) add:

    global $allowphp $allowphp = 3 // code that cycles through posts $allowphp = 0;

    it allows you to use php in your posts but keeps it from being allowed in comments, which seems to be a security concern among the sites i’ve read that talk about implementing this. there might be a more elegant way of doing it that won’t involve more than just the plugin, but i’ve only been working with wp for a few days, so i’m not too sure.

    now that i’m typing this up i suppose you can edit which html is allowed in comments, though, so i wonder if it’s even an issue!

    anyway, thanks for the code, it worked in 1.5 for me as long as i didn’t try to edit/save it through the administration panel. i poooossibly had to change the last line to

    add_filter(’the_content’, ‘run_php’, 1);

    or some such.

    anyway, if php comment usage is a problem hopefully this’ll help kids who stumble upon this in their search.

  63. Mark Says:

    AFAIK, the plugin doesn’t allow PHP in comments, it should just strip the tags (unless you have WordPress setup to allow <phpcode> tags, of course). Here’s a wee test:

    <phpcode>echo ‘this is PHP’;</phpcode>

    If anyone can highlight a situation where it does allow them though, I’d be very interested.

  64. WordPress Plugin for PHP Includes &raquo; marciahoo Says:
    <pingback />[...] ess — Marcia @ 2:56 pm Nice, with a lot of possibilities and simple instructions http://mark.scottishclimbs.com/archives/2004/07/02/running-php-in-wordpress-posts/ Comments The URI [...]
  65. Rob Says:

    I installed this plugin and (I think) I did everything correctly… However, when I try out the suggested [phpcode]echo "hello mum!";[/phpcode] it returns:

    Parse error: parse error, unexpected '&', expecting ',' or ';' in /home/radesign/public_html/words/wp-content/plugins/runphp.php(40) : eval()'d code on line 1

    WP 1.5… any ideas? This would be key to some stuff I want to be able to do with WP. Thanks!

  66. Mark Says:

    I haven’t tried the plugin with WP 1.5, but it sounds like the encoding of the quotes isn’t working correctly.

    Try using single quotes rather than double ones, then try commenting out some of the lines at the top that start: ”$data = str_replace”, they are the ones that turn the quotes into HTML entities.

    When I was debugging it, I found it useful to check the source (with the plugin disabled) to see what WP was outputting.

    Have fun :)

  67. Dennis Says:

    Hello!

    Thanks for the plugin but I can’t get it running. I uploaded the stuff and activated runphp in admin-tools. But nothing happens! Look at my site and try the Archive or the Test pages in the sidebar. Doesn’t work!

    Regards Dennis

  68. Tommy Says:

    i had problems bringing this to work under wp 1.5. i edited the specific line to “add_filter(’the_content’, ‘run_php’, 1);” but not i just get the following error:

    Warning: call_user_func_array(): First argumented is expected to be a valid callback, ‘run_php’ was given in /www/blog/wp-includes/functions.php on line 882

    dunno what to do. any ideas?

  69. Jina Says:

    This is an awesome plugin that I am using on my other website. There is one issue, and if someone has already solved it, I apologize… there were way to many comments to skim through. :)

    I’m trying to run this: include (TEMPLATEPATH . ’/searchform.php’);

    and this: wp_get_archives(‘type=monthly’);

    to create an archives page.

    I am getting these errors:

    <b>Parse error</b>: parse error, unexpected '&' in <b>/home/spit/public_html/wp-content/plugins/runphp.php(33) : eval()'d code</b> on line <b>1</b>

    AND

    <b>Parse error</b>: parse error, unexpected $, expecting T_VARIABLE or '$' in <b>/home/spit/public_html/wp-content/plugins/runphp.php(33) : eval()'d code</b> on line <b>1</b>

    Do you know of any way that I can fix this?

  70. Mark Says:

    Assuming TEMPLATEPATH is a variable of some sort, put a dollar sign directly in front of it to let PHP know that it’s a vairable that you’re talking about.

  71. Bob... Just Bob Says:

    I get these errors in admin panel

    Warning: Cannot modify header information – headers already sent by (output started at /home/flik/public_html/users/bob/wp-content/plugins/runphp.php:47) in /home/flik/public_html/users/bob/wp-admin/admin.php on line 6

    Warning: Cannot modify header information – headers already sent by (output started at /home/flik/public_html/users/bob/wp-content/plugins/runphp.php:47) in /home/flik/public_html/users/bob/wp-admin/admin.php on line 7

    Warning: Cannot modify header information – headers already sent by (output started at /home/flik/public_html/users/bob/wp-content/plugins/runphp.php:47) in /home/flik/public_html/users/bob/wp-admin/admin.php on line 8

    Warning: Cannot modify header information – headers already sent by (output started at /home/flik/public_html/users/bob/wp-content/plugins/runphp.php:47) in /home/flik/public_html/users/bob/wp-admin/admin.php on line 9

    And these errors on my pages

    Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at /home/flik/public_html/users/bob/wp-content/plugins/runphp.php:47) in /home/flik/public_html/users/bob/wp-content/themes/classic/header.php on line 2

    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at /home/flik/public_html/users/bob/wp-content/plugins/runphp.php:47) in /home/flik/public_html/users/bob/wp-content/themes/classic/header.php on line 2

  72. John Says:

    Hi,

    I love your plugin!!! For some reason it’s not taking out the line breaks. Anyone else having this problem?

    Kindly John

  73. Morten Says:

    Damn I would like to get this to work. BUT Have the same problem as comment 71. I have tryied all the things described here but with no luck :-(

  74. Mark Says:

    Have you checked there is no whitespace, as per comment 12? That seems to fix it in most cases.

  75. Morten Says:

    I have Mark and I also have tried to disable all other plugins and everything else described on this post. I have wp 1.5 and want to make it work with the mc_show_stats plugin but with no luck .

    This is my warnings;

    Warning: Cannot modify header information – headers already sent by (output started at /home/sv_blog-nokdk/wp-content/plugins/RunPHP.php:46) in /home/sv_blog-nokdk/wp-admin/admin.php on line 10

    Warning: Cannot modify header information – headers already sent by (output started at /home/sv_blog-nokdk/wp-content/plugins/RunPHP.php:46) in /home/sv_blog-nokdk/wp-admin/admin.php on line 11

    Warning: Cannot modify header information – headers already sent by (output started at /home/sv_blog-nokdk/wp-content/plugins/RunPHP.php:46) in /home/sv_blog-nokdk/wp-admin/admin.php on line 12

    Warning: Cannot modify header information – headers already sent by (output started at /home/sv_blog-nokdk/wp-content/plugins/RunPHP.php:46) in /home/sv_blog-nokdk/wp-admin/admin.php on line 13

    Another thing  My blog are in danish and we use  æ,ø and å those signs are messed up by this script
  76. Mark Says:

    Sorry Morten, I don’t know what to do :(

    I’m not using 1.5, not sure if that makes a difference or not.

  77. Morten Says:

    Huiiii :) its working

    And dispite my early statement it WAS what you said Mark . the whitespace .

    NOW I have to say ” Cool Script”

  78. Mark Says:

    Woohoo! :D

  79. Chris T. Says:

    This plugin is a thing of beauty. I was finally able to move my recommended books in Amazon Media Manager to a separate page in WordPress. Thanks for this!

  80. Richard Says:

    Nice – I just installed it and tested it on a 1.5 and it run out of the box, no problems at all.

    Exactly what I was looking for. Need to do some testing with more complicated php but I had to tell you. A great plugin.

    Many thanks

  81. Richard Says:

    I just came back to doublecheck because I had a problem with some of the php code I was trying to run.

    It turned out that adding that third parameter to add_filter(‘the_content’, ‘run_php’, 1) solved the problem. I was getting a parse error but only with certain kinds of multiline php (one of which included a number at the end of the line). Now it runs just fine.

    I am really really happy with this plugin. Matt.

    By the way it’s WP 1.5.1 that it’s doing such a great job on.

  82. Harvey Ramer Says:

    The plug-in is working well on 1.5.1.1. I installed it without modification and used it immediately.

    Thanks!

  83. Jacob Says:

    The numbers thing is broken again with WP 1.5. Tried two different systems, same problem. Most code runs fine, but as soon as you use a numerical argument the eval() fails.

    I stared at the add_filter function awhile, but I really don’t have a good enough understanding of what it does, to fix it.

    Any suggestions?

  84. Gerry Says:

    I’m trying to use the plugin to grab the users IP number when they submit a form embedded into a WP page. It worked the first time, not it’s broken…and I have no idea why :)

    The line from the form I’m trying is this:

    When I save that, WP strips out the phpcode closing tag. As you’d expect when it does that the code doesn’t work :)

    The example “hello mum” example code works fine on the page itself.</input>

  85. Gerry Says:

    See?!? It striped out phpcode end tage in my example code!

  86. skiadas Says:

    Just installed this thing, and it works great! Thanks Mark.

  87. Vik Says:

    Just installed it on WP 1.5, works great! Thank you Mark!

  88. latifk Says:

    hi still have a problem,when runing my php,when i write a code like < ?php phpinfo();php>it display nothing it just give a blanckpage or when i try to write other codes it give the codes themself

  89. jena Says:

    A friend of mine and I discovered today that the plugin only seems to work if you have something in the post/page before the <phpcode> section. If you put

    tags around your code, it works. If you just type in some text, even just a space, then your php code, it works. No idea what could be causing that, but just a warning for you folks who are having trouble with the php tags getting stripped and the post displaying the code without evaluating it.

    I wrote up a post on my blog with a little more detail on what works and what doesn’t. Hopefully it’s helpful for those of you having some problems. :)

    </phpcode>

  90. jena Says:

    Lost the link in my previous comment, but click on the jena link below this one to get to the appropriate post.

    I’ll note that I’m running WP 1.5.1.2, and aside from the aforementioned peculiarity, the plugin works great. Thanks so much for all your hard work. ;)

  91. Rob P Says:

    Im getting the same error as comment #65 I tried adding the dollar sign but it still doesnt work

    ?php include ($TEMPLATEPATH . '/searchform.php'); ?>[/phpcode]
  92. Scott Says:

    I’m getting this error:

    Parse error: parse error, unexpected ‘<’ in /home/aznews/public_html/wp-content/plugins/runphp.php(35) : eval()’d code on line 1

    After glancing through the comments, it seems like you had fixed that issue with an update of the code. I’m running WP 1.5.1.3 and was calling get_archives in a page.

    Any ideas? Thanks!

  93. Scott Says:

    Whoops. Hope it works this time.

    Here’s the code again:

    Parse error: parse error, unexpected '< ' in /home/aznews/public_html/wp-content/plugins/runphp.php(35) : eval()'d code on line 1

    In glancing through the comments, it seemed like this error had been corrected by the new code. I’m running WP 1.5.1.3 and was calling get_archives in a page.

    Any ideas? Thanks!

  94. MacBros Says:

    Hi. I got this kewel little plug in too. Mine works, but the bug I have is when I write a Page with PHP code in it, when I click Publish, I get a blank page. The page is saved and published and the PHP code is working when I go to view the results. But the blank page I get when I click Publish button is just annoying. You have a fix?

  95. MacBros Says:

    WP 1.5.1.3 BTW

  96. Patrick Says:

    Thanks for the Plugin Mark. I’m using it to display on my site an auto-generated list of all my Firefox extensions, themes and plugins (using the InfoLister extension). Very cool!

    P

  97. Matt Says:

    I installed the plugin and tried to make a page with

    <phpcode>echo “hello mum!”;</phpcode>

    and it spits out

    echo “hello mum!”;

    Is there a way to fix it?

  98. Matt Says:

    *i meant i put in

    <phpcode>echo “hello mum!”;</phpcode>

    and it puts out

    echo “hello mum!”

    btw, I’m using Wordpress 1.5.1.1

  99. Matt Says:

    nvrmind b4

    but a new question arose…

    I am having trouble running php commands that deal with the mySQL databases.

  100. Jason Golod Says:

    Thanks Mark. Took about 20 seconds to copy, paste, acivate and use. Thanks again.

  101. Kasper Says:

    How about of pages? Could you edit the code that it would work with pages and post.

  102. Kasper Says:

    How can i get the code not to skip a line?

Sorry, comments are closed for this article.