Nasser Ali Karimi
Nasser Ali Karimi

Reputation: 4663

Include PHP file from parent sibling folder in word press

I try to include some PHP file to another one by using include function. It works fine when files are at the same directory that simply can do

include (file.php)

and if a file is in the child folder like

include (folder/file.php).

but I want to make my root WordPress folder project cleaner then change the location of my template files to pages-template folder but unfortunately, I can't include files from another folder that are the sibling with pages-template. I try this

include '../inc/package-save.php';

but I got errors

Warning: include(../inc/package-save.php): failed to open stream: No such file or directory

any trick to fix this.

Upvotes: 2

Views: 2130

Answers (4)

Ronalyn Cani
Ronalyn Cani

Reputation: 1

This is for the WordPress functions.php file. Try using the following line of code:

require get_theme_file_path('/inc/file-name.php');

Upvotes: 0

Marco Valeri
Marco Valeri

Reputation: 491

if you are working with WordPress, you should use get_template_directory() like the following example:

include get_template_directory() . "/inc/package-save.php";

Upvotes: 0

Nasser Ali Karimi
Nasser Ali Karimi

Reputation: 4663

Finally, I found the solution. just put the dirname(__FILE__) function before the directory that I want to include.

This function will get the directory path that files that I write this function to it.

for example, if file be at C:\xampp\htdocs\amt-master\wp-content\themes\wp-bootstrap-child it returns this path

But as my case, I want to get the previous path to do that I should just put this function in the same function like this

include dirname(dirname(__FILE__))."/filename.php"

then it will return this path

C:\xampp\htdocs\amt-master\wp-content\themes

then you can write remaining path.

Upvotes: 9

Ally Vinod
Ally Vinod

Reputation: 1

Try to use the following

include TEMPLATEPATH . '/inc/package-save.php';

Upvotes: 0

Related Questions