How do I call external PHP files inside of a WordPress theme?

I’m working with an existing PHP web application. It’s site structure is similar to:

  • public_html
    • include
      • header.php
      • style.css
      • footer.php
    • blog

I’ve installed WordPress in /blog. I am trying to create a WordPress theme using the dynamic elements of the external PHP app.

Read More

Here is an example of /blog/wp-content/themes/custom-theme/index.php:

<?php
 include_once("../../../../include/header.php");
?>

The theme is not reproducing the header code. I’ve tried variations of the relative path, just in case, with no success. Are there other considerations I haven’t taken into account?

Related posts

Leave a Reply

2 comments

  1. If WordPress is in /blog, there’s a convenient constant called ABSPATH that holds the path to that folder. So:

    $inc_dir = dirname(ABSPATH) . '/include';   # /path/to/public_html/include
    include_once "$inc_dir/header.php";
    


    Or directly:

    include_once dirname(ABSPATH) . '/include/header.php';
    

    Seeing that you’re already using the correct relative path, though, be sure to include that file where relevant. If you’re including it in an html comment or something like that, you’ll get unexpected results.

  2. I believe the “file_get_contents” could work in this scenario too.

    It could also be handy if the external php file is part of a different program.

    “file_get_contents” is a php method which loads entire files into a string.

    See us3.php.net/file_get_contents
    A handy call in various situations.
    I used this to embed an ASP menu inside WordPress. See stackoverflow thread with an example here:
    How to Include an .asp menu file inside a php file? (WordPress Blog folder within ASP Site)