Including free-form PHP into WordPress

Update: include_HTML now has its own page.  

Every once in a while, there is a need to include free-form PHP (or HTML, or JavaScript) into a WordPress post or page (a recent post on WordPress support forums raised that problem yet again). This need can be addressed with a very simple plugin. The installation is, once again, WordPress standard; download and unzip the include-html.zip file, create an include-html directory on your WordPress server under wp-content/plugins, put include-html.php into it, and activate the plugin using WordPress’ administrative interface.

Usage is equally simple: create a file containing any combination of PHP, HTML, and/or JavaScript you want (for example, let’s say the file is accessible as http://mysite.com/myfile.php) and include it into your post or page as follows:

[include_HTML: http://mysite.com/myfile.php]

To see this plugin at work, you can look here; the “HTML file with relevant code” is included in the body of the post.

This entry was posted in Answers, Technology. Bookmark the permalink.

38 Responses to Including free-form PHP into WordPress

  1. basic html says:

    After I read this blog I understand that With only a few weeks of studying and working, users can already understand the basic features of HTMl and its useful applications in web design in the business world.

  2. MACscr says:

    I really want to use this plugin, seems perfect for me, but it doesnt seem to allow forms to work on the pages that are included. Is this possible? These are forms that would normally post to themselves.

  3. NC says:

    MACscr,

    It should be possible, but only in ugly ways; take a look at a couple of ideas here.

  4. MACscr says:

    Ignore my last comment, which your plugin is great, I was retarded and should have realized it wouldnt work for my unique need. I am using it for other sites though. Thanks!

  5. Thor Asmund says:

    I have a suggestion for an improvement which I have implemented on my own website. I have added the following line right after the line with the foreach statement in the plugin:

    $URL = str_replace(‘$QUERY_STRING’,$_SERVER[‘QUERY_STRING’], $URL);

    This allows one to add the word $QUERY_STRING in a page and have the current query string attached to the get_file_contents call thereby making it possible to transger query parameters!

    cheers,
    Thor Asmund

  6. NC says:

    Thor,

    It’s an interesting suggestion, but it would only work if WordPress were running with “ugly” URLs. With “pretty” or “semi-pretty” URLs, $_SERVER['QUERY_STRING'] is not set; WordPress figures out what to display based on $_SERVER['REQUEST_URI']

  7. Jenn says:

    Thank you. Thank you, thank you, thank you. Thank you so much for doing this. I am finally able to use the JW flash media player with the super duper fancyass javascript code. Sorry if I sound ridiculous, but I have been sitting here for hours, wracking my brain. I want to cry. Thank you, thank you.

  8. NC says:

    Jenn,

    You are very welcome. And no, you don’t sound ridiculous. WordPress (or, rather, its HTML editor) does impose certain limitations on what can be put into a post. The include_HTML plugin was written precisely to get around some of these limitations without compromising security.

  9. Kevin says:

    This is a very nice plugin and works great for HTML and JavaScript, but I cannot get it to work even for the simplest of PHP files. I tried one that simply echoed some text, and it does not appear. Do you have a working PHP example?

    Thanks!
    Kevin

  10. NC says:

    Kevin,

    Actually, I do have a working PHP example:

    http://www.myvirtualdisplay.com/2008/05/28/creating-a-faq/

  11. Cass says:

    YAY! You rock. Simple solution to annoying problem – solved!

  12. NC says:

    Cass,

    Glad you liked it! 🙂

  13. Chris says:

    Worked like a charm. But WordPress should really add a tag and allow us to add free-form HTML or a check box to turn of the editor’s annoying habit of not believing my coding.

  14. NC says:

    Chris,

    Thank you for your feedback! Incidentally, the checkbox you want already exists and has been around for quite a while now. You can find it in your user profile. In WordPress 2.3, it was called Use the visual editor when writing and was checked by default; in WordPress 2.7, it’s called Disable the visual editor when writing and is unchecked by default.

  15. YJ says:

    Hi NC,

    I am unable to add the a form object (book.php) after inserting [include_HTML: http://mysite.com/book.php%5D into page. Is there any issues with this plugin working with wordpress 2.7.1?

  16. NC says:

    YJ,

    Thank you for bringing this to my attention! The problem is real, but fixable. I just posted a workaround (actually, two workarounds) here:

    http://www.myvirtualdisplay.com/2009/03/27/include_html-gets-temperamental/

    I will update the plugin code (and bump it up to a new version) in the next few days.

  17. NC says:

    The updated (0.2) version of the plugin is now available here:

    http://www.myvirtualdisplay.com/2009/03/27/include_html-gets-a-facelift/

  18. Navjot Singh says:

    It seems your link to version 0.2 is broken. Please fix it.

  19. NC says:

    Navjot,

    Actually, it wasn’t the link that was broken, it was the post itself that needed to be fixed (it seems that I accidentally changed its publication date). But now it should be back to normal. Thank you for pointing out this problem.

  20. kurt says:

    I can’t get this plugin or PHP execution to work. I’m thinking it must have something to do with my host (1 & 1) or the PHP.ini file settings.

    Any help would be appreciated.

    Thanks!

  21. kurt says:

    I was able to get this plugin to work on a site hosted by Bluehost. Any ideas as to why it won’t run on a 1&1 hosted site?

    Thanks again!

  22. NC says:

    Kurt,

    There are at least two possibilities here.

    First is the incorrect version of include_HTML. The plugin was written for WordPress 2.3, and the initial version (0.1) has been known not to work under WordPress 2.7. If you are running WordPress 2.7 or newer, download version 0.3 from the project page:

    http://www.myvirtualdisplay.com/wordpress-projects/include_html/

    Additionally, in order for include_HTML to work, your PHP should be configured with allow_url_fopen = On. Otherwise, the plugin won’t be allowed to open remote files.

  23. Michael says:

    Great plugin. Thanks for making this available. One question, though… I’ve used it on the home page but am having issues with its placement. The link to the page is http://grant222.mscottpr.com/. I want it directly under the last paragraph to the right of the image, but as you can see, it is putting over the photo. Any thoughts or suggestions?

    Thanks

  24. Michael says:

    Please disregard my previous comment. Issue resolved. Again, nice plugin.

  25. KWeilbacher says:

    Thanks for this plugin. I used it successfully to implement Javascript to display an emag flip book on a wordpress page. I posted my notes here to help another WordPress user with the same issue.

    http://wordpress.org/support/topic/235601?replies=2

  26. VickiLH2 says:

    I’ve just installed version 0.3 for my new WP 2.8.4. I followed instructions for creating directory, activating plugin, etc..

    But my included file just doesn’t show up on the page. I’ve tried moving the file around to different directory (web root, WP /themes/mysite directory, /uploads directory). I’ve tried using different file extension, .html, .php, nothing. But it just doesn’t find it.

    Am I doing something wrong or is there a problem? thanks!! Vicki

  27. VickiLH2 says:

    Ah! Just checked php and allow_url_fopen is ‘off’. will see if this can be turned on. Thanks!!

  28. VickiLH2 says:

    Actually, in thinking about this, I doubt that the sysadmin will turn on “allow_url_fopen” due for security reasons. Just read up on this a bit, and it’s not really a good idea to turn this on. Is there some way to work around this, if any files I’d be including are local?

    thanks again! Vicki

  29. VickiLH2 says:

    Aha! I got this to work with allow_url_fopen set to OFF. I edited the include-html.php file. I substituted this:

    $replacement = '<!-- iHTMLstart -->' .
      file_get_contents($URL) .
      '<!-- iHTMLend -->';
    

    with this:

    $ch = curl_init();
    $timeout = 5; // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_URL, $URL);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);
    $replacement = '<!-- iHTMLstart -->' .
      $file_contents .         
      '<!-- iHTMLend -->';
    

    It works!! Best, Vicki

  30. Tracy says:

    Does this plugin work with pages that use CSS? I’ve tried to “include” a page that uses an external style sheet, but cannot get the styles to show. I’ve had no luck when I try them as an internal style sheet either. The page itself works fine.

  31. NC says:

    By any chance, is the link to the style sheet relative (e.g., example.css)? If so, does rewriting it as absolute (say, http://example.com/example.css) solve the problem? Also, is the inability to show style consistent across browsers?

    Also, if all else fails, you can always attempt to include the stylesheet directly:

    <style type="text/css">
    [include_html: http://example.com/example.css]
    </style>
    [include_html: http://example.com/example.htm]

    This is not the cleanest solution in that the example.html will still be asking for a non-existent stylesheet, but the styles it requires will nevertheless be defined.

  32. Tracy says:

    The link to my style sheet was relative, but making it absolute did not solve the problem. Nor did your other suggestion.

    I did upgrade the plugin to 0.4, but that did not help.

    The problem is across all browsers.

    EDIT – I was just able to specify a color, in the external CSS file, for some text. The colored text displayed as it was supposed to. It looks as if the only problem is with the background image in my style sheet. I’m using the absolute path to it. It displays on the HTML file outside of WordPress. http://www.tracyandcarol.com/html/resume.html

    But it doesn’t display when I try to include it in WP http://www.tracyandcarol.com/the_tracy_times/resumes/my-resume-test/

  33. NC says:

    It appears that you are using the plugin to produce malformed HTML. Your “Resume Test” page has two !DOCTYPE declarations, two <html> tags, two <head> tags and two <body> tags. Chances are, browsers simply don’t know what to do with two instances of the <body> tag, so the styling you apply to the <body> tag in the static HTML is simply ignored…

  34. Tracy says:

    Where do you look to see the double tags? When I look at the source code I see the code for the entire site, not just the page.

  35. NC says:

    I look at your “Resume Test” page:

    http://www.tracyandcarol.com/the_tracy_times/resumes/my-resume-test/

    If you look at the source code and search for any of the tags mentioned, you’ll find two instances of each. The outer one comes from your WordPress template files, the other, from the HTML file you include.

  36. Tracy says:

    Thank you for explaining that to me.

    I was finally able to get the results I wanted. (for the most part) http://www.tracyandcarol.com/the_tracy_times/resumes/tracys-resume/

  37. lee says:

    i wish i can adjust the font size to 1000px then i’ll write a big thank you! cheers 😀

  38. Liena Aifen says:

    Finally i found it, thanks for sharing

Leave a Reply

Your email address will not be published. Required fields are marked *