Include: Including scripts in your WordPress posts
Jambo, my dichotomous Googler. This WordPress plugin allows you to include a file of custom code in your blog entries and pages.
Installation
- Download the plugin here.
- Unzip the contents of the archive into your
wp-content/pluginsdirectory. - Activate in your WordPress dashboard.
Usage
The plugin includes a subdirectory called scripts. You place your scripts in here.
To include a script in your post, you put a <!--#include--> directive into the plain text. The syntax of this directive is different from the #include directive in the server-side include syntax in Apache or ASP/ASP.NET:
<!--#include filename [key=value [key=value] ...] -->
Any output from the script that you include will automatically be injected into the blog entry in place of the directive. Comments are not processed.
Samples
Some sample files are included to get you started. The Hello World example is the simplest, churning out a random welcome message:
<!--#include hello-world-->
Note that we have omitted the .php extension. You don’t have to do this: <!--#include hello-world.php--> works just as well. Neither have we specified a path. All files are assumed to be in the scripts subdirectory.
You can also pass some arguments to the script, which will pick them up in the global variable $args. For example, the “contact me” form needs to know who to send the e-mail message to:
<!--#include contact to="you@yourdomain.com"-->
Finally, the download box allows you to display a download box in your page. You need to give it a title, a version number, and the URL of the file to download:
<!--#include download-box title="Include" version="1.0"
url="/wp-content/plugins/include/include-1.0.0.zip" -->
Known issues
- The
<!--#include ... -->directive must not have any line breaks in it.

RSS feed


18:03
the download link is broken!
22:24
Fixed. Apologies to all!
18:54
Trying the use the samples from within LEFTMENU and it does not work. Any idea?
22:40
I think you may have misunderstood the purpose of the plugin. It’s so that you can put the include statements in the body of your blog post, not in the template itself. If you want to include another file within your template, you need to use PHP’s built-in require() and include() functions for that purpose, as in
11:26
ahh my apologies. thanks a lot
16:34
I´ve created a page and I want it to point to a specific category in wordpress.
Once I´m a newbie, I figured i could do that by using your plugin.
(í´ve moved to scripts folder and changed the links on it)
but it didn´t work . Do you have any suggestions?
Thanks.
17:40
Just a couple of general points that you need to be aware of. First, you need to disable the visual editor in WordPress 2.0 or later when you’re typing the
<!--#include-->directive into your page, otherwise it will try to encode it before saving it. Secondly, the include directive must be on a single line; if it contains line breaks, WordPress will convert these to<br />tags which will interfere with parsing it.00:10
sounds very interesting, does it work with javascripts or just php?
Lee
10:20
It’s a PHP (server side) thing, though there is nothing to stop you putting Javascript in the file that you wish to include.
23:27
Excellent – works a treat and I’m well impressed
06:27
Thanks James, this looks like it could be a big help!
14:29
can i use this for including other non-wp scripts (with db connections, data formating, query linking like bla.php?q1=qweqw&q2=erdfdf)?
and btw great plugin
za
13:01
@zdena: Yes you can. The idea is that you can include any PHP script containing arbitrary code in your posts.
There are, however, some points to note:
1. Your code will be running within the context of a PHP function. This means that if you say something such as
it won’t work as you expect. You would need to declare $foo as global at the top of your script as as well:
2. All the functions and global variables from WordPress and your plugins are available to your script. If you have any naming conflicts with these, you will experience undefined effects. Specifically, your code will be running as part of the WordPress loop.
3. If you make any changes to various PHP settings such as error_reporting, set_error_handler, or ini_set, you need to make sure that you change them back at the end of the script otherwise you may experience undefined effects. Note also that functions registered with register_shutdown_function() will run when the WordPress request ends, not when your script ends.
21:14
Hello james I am trying to do a simple include in a page of my blog and for some reason everything I have tried isn’t working? My code is below,should I move my rss2html folder into the include/scripts folder?
Thanks,
Edub9
[comment formatting tidied up - JM]
21:15
Here is the code i am using.
�w%�ס���:!����ܢo��j��͡�i��6�٥�22:17
@Edub: Unfortunately it doesn’t support remote URL includes via HTTP. You should move your rss2html file into the include/scripts folder and use this include in your post/page:
04:02
worked perfectly thanks!!!
Edub9
14:29
Excellent – this is just what I was looking for as a way to insert specific ad code in my posts. Thanks!
22:11
James, one more quick question……..
how would I use the following with your plugin or will it not work?
22:49
Hi,
I was wondering if this plugin could also be used in a template file. Or is it limited to only posts and pages within wordpress.
23:18
James I cannot get this to work. I’m sure I missed something. I uploaded the files to my plugin directory in their own directory. Do I have to activate this plugin some how? I also upload rss2html folder like you told Edub9 and used the include you gave him that worked for him. Any help would be greatly appreciated, The include simply shows up in my blog source as a comment.
23:20
<!--#include hello-world-->Does not work for me either.
23:25
I found my problem. When extracting the archive supplied I get this as folders.
include-1.0.0\include
I uploaded include-1.0.0 to my plugins directory when the correct folder to upload is include.
I moved the folder and I was able to then activate the plugin on my dashboard and now works perfectly a real life saver this one is.
Well it works with the hello world script I get this when running rss2html
“Unable to open template sample-template.html, exiting ” Any one experience this problem?
21:39
James, first of all, it’s a great plugin and thanks for that. I am trying to use this plugin, it works well but my script what I use inside wordpress page doesn’t display unicode character. While same script works fine separately (display unicode characters).
I am querying mysql db and displaying result, that script works well separate;y but doesn’t display unicode characters using this plaugin. Wordpress support & works for uncicode character as well.
I also tried to display unicode characters in wordpress post and page by just typing, just to see it display or not, it worked there as well.
Can you please help, if it has anything to do with this plugin. Thanks
13:29
Hey, could you tell me how to include multipage scripts into WP?
I’ve got this script thats an entire folder, and starts with index.php in it.
How can I integrate this as a post in WP (it uses no DB functions)?
19:05
Great plugin does an awesome job for an obvious lack (page-by-page server-side scripting includes) in WP. May I suggest a terrific way to improve it? Provide a textfield window on the edit page for a specific page. When I enter a script into the textarea, the textfield can be saved as in the plugins/include/scripts directory.
That way, we can cut and paste a script into the textarea, save it to a specific filename, and then without ever leaving the editor.
I’m willing to pitch in a few hours of coding assistance if you are interested. In any case, cheers and kudos for a nice plugin – great work!
18:41
Hi James,
I’m sure this should be a great plugin, but when I use your sample or any other php script I recieve this error:
Method Not Implemented
POST to /wp-admin/post.php not supported.
The same happens with pages.
Any suggestions?
Thanks a lot.