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.

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

  6. shy says:

    issue seems not resolved.
    actually there is a php application inside a directory and i want to include that entire application.

  7. Mark says:

    Many thanks for that, Andy! I was scratching my head for ages wondering why I couldn’t get a regular php include to work. Brilliant stuff!

  8. Andy, THX!
    just was I was desperately looking for. Great. thanks for posting your knowledge!

  9. Derek says:

    With this in mind, I want to include a private WP page in a sidebar widget using php include, can someone help me with that?

    Basically, I have a band director that wants to post updates really easily since he’s not computer savy, so I thought he could just add text to a WP page that is not listed in the list of pages (making it private) and then the sidebar will display that page on every page… hope that make sense and someone can help me out.

    Thanks,
    Derek

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>