I’m working on a project that runs with WordPress – So, all the project files sit in a folder named WP_project in the wordpress folder.
Now, not all users have wordpress installed in the root, but in a folder (like for a sub-domain for example).
So I can simply check (during installation) if the wordpress folder is in the root, and if it is, great, if not, I’ll have to pause the installation and ask the user to give the correct path to it, and thats the problem.
One – Not all users are computer savy, and their hosts may have put the WP folder somewhere else without informing the user, so they may not know where it is, or worse – they may not know what they’re even being asked to do…
Two – Even if I do some how find the WP folder, how would I implement it into every single script that needs to know? For example, this is what I’ve got on every script (in the project):
if(file_exists($_SERVER['DOCUMENT_ROOT'].'/wordpress')){
$folder_rt=$_SERVER['DOCUMENT_ROOT'].'/wordpress';
}elseif(!file_exists($_SERVER['DOCUMENT_ROOT'].'/wordpress')){
$folder_rt=$_SERVER['DOCUMENT_ROOT'];
}
So what that does is; 1 Check if the wordpress folder exists in the root. 2 If it does, great, assign $folder_rt to its path, but if it doesn’t; just assign $folder_rt to the root path (assuming that WordPress is installed in the root).
So if i do find the folder path (eventually), how would I assign $folder_rt to it in every script. I can’t save the path in a separate file, because then I would have to include/require that separate file, in every script – which cannot be done, without the path to the folder (using absolute paths). Its the same with storing the path in a DB, I’d have to make a separate file for the function to check the DB (because I don’t think its safe storing sensitive DB info in every script…).
I’ve kind of come up with a solution, but I believe there must be a better way, and its not really been thought through;
On installation, if the WP folder cant be found, ask the user for the path, then, store that path in a separate file, in the root. Then, I can simply check for that file on every script…so instead of the code example above, I could have something like this in every script:
if(file_exists($_SERVER['DOCUMENT_ROOT'].'/wordpress')){
$folder_rt=$_SERVER['DOCUMENT_ROOT'].'/wordpress';
}elseif(file_exists($_SERVER['DOCUMENT_ROOT'].'/wp-config')){
$folder_rt=$_SERVER['DOCUMENT_ROOT'];
}
elseif(file_exists($_SERVER['DOCUMENT_ROOT'].'/project_pathfile.php')){
require_once($_SERVER['DOCUMENT_ROOT'].'/project_pathfile.php');
$folder_rt=$path_from_sep_file;
}
There must be a better, more professional way of doing this right?
Anny suggestions? Thank you
dirname(__FILE__)(or just__DIR__in 5.3+, IIRC) will give you the name of the directory the current script is in. If the files will always be in an immediate subdirectory of the wordpress folder, thendirname(dirname(__FILE__))(ordirname(__DIR__)) should give you the location of the wordpress directory. No configuration necessary.