phpwordpressinclude-path

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.


Solution

  • 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.