Jared Forth
Jared Forth

Reputation: 1637

PHP "warning include_once(): Failed to open stream" in Wordpress theme

I am building a WordPress theme, and have broken up functions I am using to customize the theme into a library sub-directory in the theme root and including them with the include_once() function.

I have further broken up files into sub-directories inside of library and including each file with the correct path.

I am including close to 20 files in this way with no issues, but I have one file, locations-post-type.php, in the library/post-types directory that is triggering the following errors when included:

Warning: include_once(library/post-types/locations-post-type.php ): failed to open stream: No such file or directory in MY-THEME-PATH/wp-content/themes/caris/functions.php on line 98

Warning: include_once(): Failed opening 'library/post-types/locations-post-type.php ' for inclusion (include_path='.:/opt/alt/php70/usr/share/pear') in MY-THEME-PATH/wp-content/themes/caris/functions.php on line 98

There are 8 files in the /library/post-types subdirectory, all of which have identical permissions and ownership. Running the ls -l command returns:

-rw-rw-r-- 1 rivalmi1 rivalmi1 5930 Jun 10  2019 careers-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 5883 Jun 10  2019 events-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 3066 Jun 10  2019 faqs-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 3115 Jun 10  2019 history-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 7181 Jun 10  2019 locations-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 5121 Jun 10  2019 team-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 3164 Jun 10  2019 testimonials-post-type.php
-rw-rw-r-- 1 rivalmi1 rivalmi1 6031 Jun 10  2019 workshops-post-type.php

I do not see any difference in the locations-post-type.php file from all the others that I can include_once() with no errors.

Also locations-post-type.php works fine on my XAMPP localhost server, and only causes an error on my staging server.

Does anyone know what could be causing this issue?

Upvotes: 1

Views: 4074

Answers (1)

Andrew Real
Andrew Real

Reputation: 79

Try using get_template_directory() (or get_stylesheet_directory() for a child theme) to ensure you're requiring the file from the location you intend.

require_once( trailingslashit( get_template_directory() ) . 'library/post-types/locations-post-type.php' );

Upvotes: 1

Related Questions