This took longer than I thought to figure out. I wanted to use a server side include so I can use files on my webserver, but that are not within my WordPress installation. I tried a bunch of stuff, but luckily was able to see the error messages when it failed by viewing the source code of the WordPress blog. For instance, I got “syntax error, unexpected T_STRING”. This is the right code (assuming, of course, that the file you want is on the same server, but just in a directory outside the WordPress installation):

<?php include(ABSPATH . ‘/filename.html’); ?>

ABSPATH means, as you can no doubt imagine, absolute path. So if the file you wanted was in a sub directory such as “myfiles” you’d change the above to:

<?php include(ABSPATH . ‘/myfiles/filename.html’); ?>

You’d typically be calling for a file with an extension such as .html, .htm or .php. I need to do this as I plan to merge a new WordPress blog into an existing site, and the site has CSS style menus driven with an unordered list of links in a separate html file which I only want to have to edit in the one place.

5 Comments to “How to use a PHP include with WordPress”

  1. Graham says:

    Thank you SOOO much, I have been looking for these answers.

  2. Jeff says:

    Sweeeeet.. Thanks man! Just what I needed!

  3. Ben Tremblay bentrem.sycks.net says:

    Has WWW jumped the short /or what?!/
    This has puzzled me for the longest time … but I didn’t bother googling, imagining 10 tons of unrelated results.
    But just now I did …
    … and got this; spot on!

    thanks so much
    @bentrem

  4. Pixi says:

    Thanks for posting this! Been trying to work this out for ages. :o )

  5. [...] 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 [...]

Leave a Reply

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