How can I retrieve the path to the root directory in WordPress CMS?
Note: This answer is really old and things may have changed in WordPress land since.
I am guessing that you need to detect the WordPress root from your plugin or theme. I use the following code in FireStats to detect the root WordPress directory where FireStats is installed a a WordPress plugin.
function fs_get_wp_config_path()
{
$base = dirname(__FILE__);
$path = false;
if (@file_exists(dirname(dirname($base))."/wp-config.php"))
{
$path = dirname(dirname($base))."/wp-config.php";
}
else
if (@file_exists(dirname(dirname(dirname($base)))."/wp-config.php"))
{
$path = dirname(dirname(dirname($base)))."/wp-config.php";
}
else
$path = false;
if ($path != false)
{
$path = str_replace("\\", "/", $path);
}
return $path;
}
You can use get_site_url() function to get the base url of the wordpress site.
For more information, please visit http://codex.wordpress.org/Function_Reference/get_site_url
Looking at the bottom of your wp-config.php file in the wordpress root directory will let you find something like this:
if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/');
For an example file have a look here:
http://core.trac.wordpress.org/browser/trunk/wp-config-sample.php
You can make use of this constant called ABSPATH in other places of your wordpress scripts and in most cases it should point to your wordpress root directory.
This an old question, but I have a new answer. This single line will return the path inside a template: :)
$wp_root_path = str_replace('/wp-content/themes', '', get_theme_root());
Try this function for get root directory path:
get_template_directory_uri();
echo ABSPATH;
// This shows the absolute path of WordPress
ABSPATH
is a constant defined in the wp-config.php file.