4

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

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?

4
  • What about: include( 'twitter-feed.php' )? What does that produce? Commented Apr 14, 2015 at 12:16
  • 2
    I think you misunderstand include, it is not relative to your websites URL, it is relative to your server directory, so hardcoding the full thing will look more like include '/var/www/mywordpresssite... Commented Apr 14, 2015 at 12:25
  • Still looking for a solution? Commented Apr 15, 2015 at 5:18
  • 1
    get_stylesheet_directory_uri will get you an URI, but you want a local file system path. And there’s a function for that as well – get_stylesheet_directory Commented Apr 15, 2015 at 21:25

3 Answers 3

7

You're trying to include by URL. You need to include by the file's path (not the uri):

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

Read more about get_stylesheet_directory() in the docs.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

-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'); ?>

1 Comment

It seems TEMPLATEPATH is deprecated in fact, we should use get_template_directory() instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.