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.

4 Responses to How to: Include external PHP file in WordPress
  1. ItsAdam

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

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

    @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. Joff

    Cheers for the heads-up John, I’ve now updated the code snippet :)

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.

*

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>