Retrieve WordPress root directory path?

Wordpress

Wordpress Problem Overview


How can I retrieve the path to the root directory in WordPress CMS?

Wordpress Solutions


Solution 1 - Wordpress

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.

Solution 2 - Wordpress

echo ABSPATH; // This shows the absolute path of WordPress

ABSPATH is a constant defined in the wp-config.php file.

Solution 3 - Wordpress

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;
}

Solution 4 - Wordpress

There are 2 answers for this question Url & directory. Either way, the elegant way would be to define two constants for later use.

define (ROOT_URL, get_site_url() );
define (ROOT_DIR, get_theme_root() );

Solution 5 - Wordpress

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());

Solution 6 - Wordpress

   Please try this for get the url of root file.
   

First Way:

 $path = get_home_path();
   print "Path: ".$path; 
// Return "Path: /var/www/htdocs/" or
 
// "Path: /var/www/htdocs/wordpress/" if it is subfolder

Second Way:

And you can also use 

    "ABSPATH"

this constant is define in wordpress config file.

Solution 7 - Wordpress

> theme root directory path code

 <?php $root_path = get_home_path(); ?> 
print "Path: ".$root_path;

> Return "Path: /var/www/htdocs/" or "Path: > /var/www/htdocs/wordpress/" if it is subfolder > > Theme Root Path

 $theme_root = get_theme_root();
 echo $theme_root

> Results:- /home/user/public_html/wp-content/themes

Solution 8 - Wordpress

For retrieving the path you can use a function <?php $path = get_home_path(); ?>. I do not want to just repeat what had been already said here, but I want to add one more thing:

If you are using windows server, which is rare case for WordPress installation, but still happens sometimes, you might face a problem with the path output. It might miss a "" somewhere and you will get an error if you will be using such a path. So when outputting make sure to sanitize the path:

<?php 

$path = get_home_path(); 
$path = wp_normalize_path ($path);

// now $path is ready to be used :)

?>

Solution 9 - Wordpress

I think this would do the trick:

function get_wp_installation()
{
	$full_path = getcwd();
	$ar = explode("wp-", $full_path);
	return $ar[0];
}

Solution 10 - Wordpress

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;
}

Solution 11 - Wordpress

If you have WordPress bootstrap loaded you can use get_home_path() function to get path to the WordPress root directory.

Solution 12 - Wordpress

I like the accepted answer and @cfx's solution, but they can be consolidated a bit more:

function base_dir () {
	$path = dirname(__FILE__);
	while (true) {
		if (file_exists($path."/wp-config.php")) {
			return $path."/";
		}
		$path = dirname($path);
	}
}

This allows for you to find the base directory in files that are not loaded by WordPress, such as dynamically-created javascript and css files.

Solution 13 - Wordpress

Why was this made so complicated? One command:

Nginx

grep -i 'root' /etc/nginx/sites-enabled/*

Apache

grep -i 'DocumentRoot' /etc/apache2/sites-enabled/*

Solution 14 - Wordpress

Here are the various WorPress solutions get the directory. You can pick anyone as per the need.

 echo "<br/>".get_home_url();     // https://mysiteurl.com

 echo "<br/>".ABSPATH;            // /app/

 echo "<br/>".get_home_path();    // /app/

 echo "<br/>".get_site_url();     // https://mysiteurl.com

 echo "<br/>".get_template_directory(); // /app/wp-content/themes/mytheme

 echo "<br/>".dirname(__FILE__);  // /app/wp-content/plugins/myplugin/includes

 echo "<br/>".get_theme_root();   // /app/wp-content/themes

 echo "<br/>".plugin_dir_path( __FILE__ ); // /app/wp-content/plugins/myplugin/includes/

 echo "<br/>".getcwd();           // /app/wp-admin

Solution 15 - Wordpress

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

Solution 16 - Wordpress

Try this function for get root directory path:

get_template_directory_uri();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAadiView Question on Stackoverflow
Solution 1 - WordpressstefanglaseView Answer on Stackoverflow
Solution 2 - WordpressJaya KumaView Answer on Stackoverflow
Solution 3 - WordpressOmry YadanView Answer on Stackoverflow
Solution 4 - WordpressNatyView Answer on Stackoverflow
Solution 5 - WordpressyitwailView Answer on Stackoverflow
Solution 6 - WordpressRitesh d joshiView Answer on Stackoverflow
Solution 7 - WordpressVivek TamrakarView Answer on Stackoverflow
Solution 8 - WordpressNick SurmanidzeView Answer on Stackoverflow
Solution 9 - WordpressDũng Trần TrungView Answer on Stackoverflow
Solution 10 - WordpresscfxView Answer on Stackoverflow
Solution 11 - WordpressSisirView Answer on Stackoverflow
Solution 12 - WordpresskloddantView Answer on Stackoverflow
Solution 13 - WordpressMike CiffoneView Answer on Stackoverflow
Solution 14 - WordpressNishad UpView Answer on Stackoverflow
Solution 15 - WordpressArun Vasudevan NairView Answer on Stackoverflow
Solution 16 - WordpressHeena PatelView Answer on Stackoverflow