Include: Including scripts in your WordPress posts

Ce'ad mi'le fa'ilte, you little robot. This WordPress plugin allows you to include a file of custom code in your blog entries and pages.

Installation

Include
version 1.0
Download now Download now
  1. Download the plugin here.
  2. Unzip the contents of the archive into your wp-content/plugins directory.
  3. 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.

19 comments:

  • giorgio
    12 Jan 2007
    18:03

    the download link is broken!

  • 12 Jan 2007
    22:24

    Fixed. Apologies to all!

  • 17 Jan 2007
    18:54

    Trying the use the samples from within LEFTMENU and it does not work. Any idea?

  • 17 Jan 2007
    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

    <?php require(dirname(__FILE__) . '/hello.php'); ?>
  • 18 Jan 2007
    11:26

    ahh my apologies. thanks a lot

  • Josh
    3 Mar 2007
    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.

  • 3 Mar 2007
    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.

  • lee
    8 Mar 2007
    00:10

    sounds very interesting, does it work with javascripts or just php?

    Lee

  • 8 Mar 2007
    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.

  • Dave
    15 Mar 2007
    23:27

    Excellent - works a treat and I’m well impressed cool

  • 27 May 2007
    06:27

    Thanks James, this looks like it could be a big help!

  • 28 Sep 2007
    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

  • 9 Oct 2007
    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

    $foo = "bar";
    function baz() {
        global $foo;
        print $foo;
    }
    baz();

    it won’t work as you expect. You would need to declare $foo as global at the top of your script as as well:

    global $foo;
    $foo = "bar";
    function baz() {
        global $foo;
        print $foo;
    }
    baz();

    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.

  • Edub9
    6 Mar 2008
    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?

    <!--#include "http://johnhawman.com/sleepap/rss2html/rss2html.php"-->

    Thanks,

    Edub9

    [comment formatting tidied up - JM]

  • Edub9
    6 Mar 2008
    21:15

    Here is the code i am using.

    Šw%¹×¡¶Úþ:!ž°™©Ü¢oì•ç©jŸë²Í¡¶i®Ë6†Ù¥¦

  • 6 Mar 2008
    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:

    <!--#include rss2html/rss2html.php -->
  • Edub9
    7 Mar 2008
    04:02

    worked perfectly thanks!!!

    Edub9

  • 7 Mar 2008
    14:29

    Excellent - this is just what I was looking for as a way to insert specific ad code in my posts. Thanks!

  • 22 May 2008
    22:11

    James, one more quick question……..

    how would I use the following with your plugin or will it not work?

RSS feed for comments on this entryAdd your comments



(Personal blogs only please: leave blank if you don't have one)

Your comments:

-) razz mad lol cool ??? shock sad smile

Maximum 2 links per comment. Do not use BBCode.