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
ItsAdam September 22, 2010
You can include via your Themes Template folder too,
Here’s an example:
< ?php include( TEMPLATEPATH . '/includes/home-slider.php'); ?>
TEMPLATEPATH = wp theme folder
Shahrukh March 8, 2011
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.
John December 9, 2011
@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.
Joff December 12, 2011
Cheers for the heads-up John, I’ve now updated the code snippet