95

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

Dr Upvote
  • 8,023
  • 24
  • 91
  • 204
Aadi
  • 6,959
  • 28
  • 100
  • 145

16 Answers16

163

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.

Maxime
  • 8,645
  • 5
  • 50
  • 53
stefanglase
  • 10,296
  • 4
  • 32
  • 42
  • 10
    this is only useful if wp-config.php have already been included. in some cases (specifically an out of context ajax call to your plugin code) this does not hold true. – Omry Yadan Mar 01 '10 at 14:43
  • 19
    You should be using the built-in WordPress AJAX handler to manage all AJAX calls. – Lance Cleveland Feb 07 '13 at 04:56
  • 6
    Update: ABSPATH is now defined in the wp-load.php file which is always included therefore the inclusion of wp-config.php doesn't make a difference anymore – Benjamin Nov 12 '18 at 11:02
  • @Lance Cleveland There are other situations where you would not have already loaded WordPress, such as if you have a dynamically-generated javascript or css file that needs to find the location of wp-load.php so that it can use the get_option or get_theme_mod functions. – kloddant Jun 11 '21 at 13:57
74

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

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

Mahmood Rehman
  • 4,303
  • 7
  • 39
  • 76
Jaya Kuma
  • 749
  • 5
  • 2
29

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;
}
Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
  • cool. just remember to rename the function so we don't have function name collision if both plugins are installed :) – Omry Yadan Mar 02 '10 at 13:12
  • 2
    This is good but since the OP asked for the root directory make sure that the both of the `$path = dirname(...` lines do NOT end with `."/wp-config.php"` – Serj Sagan Jun 12 '12 at 22:35
  • 2
    Note that wp-config.php can be one placed one folder above wordpress installation. Using wp-load.php or wp-login.php might be a better option. – Dakshinamurthy Karra Sep 16 '12 at 06:52
  • 3
    would be more useful for others if you would change the filename wp-config.php into an variable that should be passed to the function when called like `get_wp_path('wp-config.php');` – Daniël Tulp Apr 30 '13 at 18:25
  • why not while(!@file_exists($base."/wp-config.php")) $base=dirname($base); – Praesagus Sep 20 '13 at 19:00
  • plugins can be in wp-content/plugins/PLUGIN_NAME/ or wp-content/plugins/plugin-name.php, no need to look further (may even return incorrect result if your plugin is incorrectly installed). – Omry Yadan Sep 27 '13 at 16:26
  • 21
    No no no! How can that be an accepted answer? Not only the OP didn't mention about his code being in a plugin, but relying on such a frail code should always be avoided. Wordpress structure may evolve. If you really need to know Wordpress' root dir, maybe there's a conception mistake. Try top store your file somewhere else that will be at a known relative path from the file you're loading it from. Anyway, if you still really need to play with the root dir, Wordpress 3 now features the ABSPATH constant, which has what you are looking for. For prior versions, just check the code of wp-load.php – Ninj Feb 19 '14 at 20:35
  • this code is working since 2006 or so, so it's not so frail. No doubt Wordpress have evolved and there might be a better way to do it now. ABSPATH is not new and as far as I remember was not a good solution but it's been quite some time since I wrote this. – Omry Yadan Feb 20 '14 at 17:20
  • 1
    just use the already available ABSPATH constant. the accepted answer should be the one below by Jaya Kuma. – DrLightman Oct 27 '14 at 23:28
  • if you need to load the config, you cannot rely on the ABSPATH because it's not loaded yet. one example when you need to do something like this is when you perform an AJAX call directly into your plugin. – Omry Yadan Oct 28 '14 at 22:06
  • @OmryYadan But shouldn't all AJAX calls be made via admin-ajax.php so that they are routed through WordPress for security, and functionality reasons? – Phill Healey Jul 03 '15 at 12:47
  • @PhillHealey, maybe. this answer over 5 years old now. things may have changed. – Omry Yadan Jul 04 '15 at 19:31
  • @OmryYadan That's a fair point. I didn't notice that it was quite so old. ;-) – Phill Healey Jul 06 '15 at 17:49
  • Be aware that some sites have configured plugin path WP_CONTENT_DIR or WP_PLUGIN_DIR. The code above looks in parent folders for a file that is known to be in root. But if WP_CONTENT_DIR is set, your theme or plugin might not reside in a subfolder to the root folder – rosell.dk Oct 25 '18 at 06:11
10

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() );
Sisir
  • 2,668
  • 6
  • 47
  • 82
Naty
  • 575
  • 5
  • 7
9

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());
yitwail
  • 1,999
  • 3
  • 20
  • 27
  • 1
    That assumes the templates are until wp-content, which may not be the case. You can redefine the name of the content directory when you install WP. It is not generally recommended as many plugins make this same assumption and break, but the possibility is there. However, there is no point continuing to repeat those incorrect assumptions here. – Jason Feb 05 '13 at 13:57
  • Honestly, I wasn't aware wp-content could be renamed. In that case, you could change `wp-content` in my code snippet to the modified name and it would still work. – yitwail Feb 14 '13 at 18:21
8
   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.
Ritesh d joshi
  • 813
  • 1
  • 12
  • 15
7

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

Vivek Tamrakar
  • 502
  • 8
  • 18
6

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
Nishad Up
  • 3,457
  • 1
  • 28
  • 32
3

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 :)

?>
Nick Surmanidze
  • 1,671
  • 1
  • 11
  • 20
2

I think this would do the trick:

function get_wp_installation()
{
    $full_path = getcwd();
    $ar = explode("wp-", $full_path);
    return $ar[0];
}
Dũng Trần Trung
  • 6,198
  • 3
  • 24
  • 20
  • This cannot be used in a plugin as it is possible to configure Wordpress to place the plugin folder outside the Wordpress root. Also beware that you should not use such method if the purpose is to dynamically load "wp-load.php" as this isn't secure. – rosell.dk Aug 13 '19 at 08:17
2

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;
}
cfx
  • 3,311
  • 2
  • 35
  • 45
0

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

Sisir
  • 2,668
  • 6
  • 47
  • 82
0

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.

kloddant
  • 1,026
  • 12
  • 19
  • if the function cannot find any wp-config.php in all levels, what will happen? – xjlin0 Jul 25 '21 at 16:05
  • 1
    Infinite loop then, but assuming you are using this function in a WordPress installation, that should never happen because the wp-config.php file will always be there. If you are concerned about this though, you could always add a counter and stop the loop after a certain threshold if you want. – kloddant Jul 27 '21 at 13:21
0

Why was this made so complicated? One command:

Nginx

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

Apache

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

Mike Ciffone
  • 281
  • 2
  • 6
-2

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

-4

Try this function for get root directory path:

get_template_directory_uri();