In my template file I want to include a library. I uploaded the library into my theme folder, and inside my theme folder is my custom template file. In my custom template file I have require 'OAuth2/Client.php';
but when I load a page that uses the template, it gives me this:
Warning: require(OAuth2/Client.php): failed to open stream: No such
file or directory in
/home/healthf0/public_html/wp-content/themes/healthfitcorpwell/single-iframe.php
on line 4
Obviously the file does exist, I can see it sitting there via FTP. Why is it telling me this?
You are seeing this error because you are require
ing the file with a relative path. As @Mark Kapulun pointed out in the comments you should not use relative paths when require
ing files. Instead you want to be explicit and use absolute paths.
Use get_template_directory() which returns the
Absolute path to the directory of the current theme (without the trailing slash)
In your template file the require statement will look like:
require( get_template_directory() . '/path/from/theme/root/to/file.php');
Or get_stylesheet_directory()
if you are making a child theme. doc
Update:
As @Jack Johansson noted in his answer, you may also consider using require_once
instead. This will protect you from errors generated by multiple inclusions of the file. See this answer for more details.