Tom Geoco
Tom Geoco

Reputation: 815

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:

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

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?

Upvotes: 1

Views: 14847

Answers (2)

Martin Sansone - MiOEE
Martin Sansone - MiOEE

Reputation: 4399

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)

Upvotes: 0

Denis de Bernardy
Denis de Bernardy

Reputation: 78473

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.

Upvotes: 4

Related Questions