How can I retrieve the path to the root directory in WordPress CMS?
I like @Omry Yadan's solution but I think it can be improved upon to use a loop in case you want to continue traversing up the directory tree until you find where wp-config.php
actually lives. Of course, if you don't find it and end up in the server's root then all is lost and we return a sane value (false
).
function wp_get_web_root() {
$base = dirname(__FILE__);
$path = false;
while(!$path && '/' != $base) {
if(@file_exists(dirname($base)).'/wp-config.php') {
$path = dirname($base);
} else {
$base = dirname($base);
}
}
return $path;
}