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.

38 thoughts on “Including free-form PHP into WordPress

  1. basic html

    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

    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. MACscr

    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!

  4. Thor Asmund

    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

  5. NC Post author

    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']

  6. Jenn

    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.

  7. NC Post author

    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.

  8. Kevin

    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

  9. Chris

    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.

  10. NC Post author

    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.

  11. NC Post author

    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.

  12. kurt

    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!

  13. kurt

    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!

  14. NC Post author

    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.

  15. Michael

    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

  16. VickiLH2

    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

  17. VickiLH2

    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

  18. VickiLH2

    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

  19. Tracy

    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.

  20. NC Post author

    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.

  21. Tracy

    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/

  22. NC Post author

    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…

  23. Tracy

    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.

Leave a Reply

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