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.
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.
basic html
19 Sep 08 at 3:58 am
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.
MACscr
2 Nov 08 at 12:52 am
MACscr,
It should be possible, but only in ugly ways; take a look at a couple of ideas here.
NC
2 Nov 08 at 10:43 am
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!
MACscr
8 Nov 08 at 12:27 pm
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
Thor Asmund
9 Nov 08 at 4:50 am
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']…
NC
9 Nov 08 at 4:34 pm
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.
Jenn
25 Feb 09 at 10:09 am
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.
NC
25 Feb 09 at 2:17 pm
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
Kevin
10 Mar 09 at 10:06 pm
Kevin,
Actually, I do have a working PHP example:
http://www.myvirtualdisplay.com/2008/05/28/creating-a-faq/
NC
10 Mar 09 at 10:19 pm
YAY! You rock. Simple solution to annoying problem – solved!
Cass
18 Mar 09 at 7:24 am
Cass,
Glad you liked it!
NC
18 Mar 09 at 12:04 pm
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.
Chris
18 Mar 09 at 1:51 pm
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.
NC
18 Mar 09 at 3:22 pm
Hi NC,
I am unable to add the a form object (book.php) after inserting [include_HTML: http://mysite.com/book.php into page. Is there any issues with this plugin working with wordpress 2.7.1?
YJ
27 Mar 09 at 9:01 am
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.
NC
27 Mar 09 at 11:34 am
The updated (0.2) version of the plugin is now available here:
http://www.myvirtualdisplay.com/2009/03/27/include_html-gets-a-facelift/
NC
27 Mar 09 at 11:59 am
It seems your link to version 0.2 is broken. Please fix it.
Navjot Singh
7 Apr 09 at 1:55 pm
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.
NC
7 Apr 09 at 5:03 pm
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!
kurt
17 Jun 09 at 9:47 am
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!
kurt
17 Jun 09 at 11:09 am
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.
NC
18 Jun 09 at 6:48 am
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
Michael
30 Jun 09 at 7:31 am
Please disregard my previous comment. Issue resolved. Again, nice plugin.
Michael
30 Jun 09 at 9:28 am
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
KWeilbacher
14 Jul 09 at 6:10 am
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
VickiLH2
5 Sep 09 at 10:58 am
Ah! Just checked php and allow_url_fopen is ‘off’. will see if this can be turned on. Thanks!!
VickiLH2
5 Sep 09 at 11:11 am
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
VickiLH2
5 Sep 09 at 11:18 am
Aha! I got this to work with allow_url_fopen set to OFF. I edited the include-html.php file. I substituted this:
with this:
It works!! Best, Vicki
VickiLH2
5 Sep 09 at 12:01 pm