Cannot include PHP file from WordPress theme folder

I’m trying to include a PHP file which is in the root of my WordPress theme file.

E.g localhost/mywordpresssite/wp-content/themes/mytheme/myfile.php

Read More

I’m trying to call it from a file footer.php which is located in parts > shared

E.g localhost/mywordpresssite/wp-content/themes/mytheme/parts/shared/footer.php

I can confirm that when visiting the full URL in the browser, the file does show, so it’s definitely the right path.

I have been unable to include this file in a number of different ways:

1) Preferred way – using the stylesheet directory URI

<?php
    $stylesheet_root = get_stylesheet_directory_uri();
    include $stylesheet_root.'/myfile.php';
?>

This should find the stylesheet URI and then find the file, but I get this error:

no suitable wrapper could be found

I understand the reason this happens is that it is not secure and that allow_url_include is turned off by default, so what’s the best way?

2) Coding in the URL to climb up two directories

This seems like a fairly sensible way because the files will always be relative two directories apart from each other. But this seems to do nothing. It’s still looking for the file within the same directory:

<?php include '../../myfile.php'; ?>

3) Just hardcode the stupid thing in.

Yet this STILL doesn’t work, even though the URL placed here is tested in the browser and does successfully load the right file. It won’t load it into the file where the include is though…

<?php include 'localhost/mywordpresssite/wp-content/themes/mytheme/twitter-feed.php'; ?>

Gives the error:

Warning: include(localhost/mywordpresssite/wp-content/themes/mytheme/twitter-feed.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/mywordpresssite/wp-content/themes/mytheme/parts/shared/footer.php on line 3

What else can I do?

Related posts

Leave a Reply

3 comments

  1. It seems like you are using a relative path, ie. you don’t have the starting /. The path should look similar to (unix/mac) /var/www/mywordpresssite/wp-content/themes/mytheme/twitter-feed.php.

    You can use include(__DIR__ . '/myfile.php'); so your includes are always relative to the directory your script is currently in.

    For some reason using ../../myfile.php will not work for includes.

  2. I put hello.php in the root folder of my theme (where footer.php is) and this works for me at the end of my footer.php:

    <?php include (TEMPLATEPATH . '/hello.php'); ?>