How to: Include external PHP file in WordPress

So, you want to include (also include_once, require or require_once) another PHP file in one of your WordPress files?  Fine, but if your PHP file is in a different folder/directory to your WordPress installation you might run into problems.

After spending too long trawling the WordPress Codex trying to find a solution, I luckily found this post by Andy Fletcher – How to use a PHP include with WordPress

ABSPATH is your friend and the answer to your prayers.   For example, assume your PHP files are in a folder off your web root called “thefolder”, you would include them using:

<?php include(ABSPATH . '/thefolder/thefile.php'); ?>

Simple when you know how.

5 thoughts on “How to: Include external PHP file in WordPress”

  1. You can include via your Themes Template folder too,

    Here’s an example:


    < ?php include( TEMPLATEPATH . '/includes/home-slider.php'); ?>

    TEMPLATEPATH = wp theme folder

  2. when ever I use the above function “” it comes out with an error “Parse error: syntax error, unexpected ‘/’ in /path/to/dir/wp/wp-content/themes/MYTheme/dir/test.php on line some where and when I check the file where I put the code above it shows “<?php include(ABSPATH . " nothing after it, pls help if you can.

  3. @Shahrukh…

    Did you copy/paste directly from here?

    It sounds like you’re missing the apostrophe – if you copied from here then you want to change it.

  4. That’s great! But what if one has a bunch of wordpress sites on the same server, and wants to include one file in all of the wordpress sites? ABSPATH only gets us up to the top level of the wordpress site.

    /server/local/home/myaccount/example.com/

    but if we have
    /server/local/home/myaccount/example1.com/
    /server/local/home/myaccount/example2.com/

    and want to include

    /server/local/home/myaccount/cookies/cookiefile.php/

    what then?

    A bridge include at the root of each WordPress site works.

    /server/local/home/myaccount/bridge.php/

    bridge.php says “include(‘/server/local/home/myaccount/cookies/cookiefile.php’)

    Now, everytime we update cookies/cookefile.php, all of example.com, example1.com and example2.com get updated at once.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>