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.
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.
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 :)
?>
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).
Looking at the bottom of your wp-config.php file in the wordpress root directory will let you find something like this:
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.
echo
ABSPATH;
// This shows the absolute path of WordPressABSPATH
is a constant defined in the wp-config.php file.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.
There are 2 answers for this question Url & directory. Either way, the elegant way would be to define two constants for later use.
This an old question, but I have a new answer. This single line will return the path inside a template: 🙂
First Way:
Second Way:
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:
Here are the various WorPress solutions get the directory. You can pick anyone as per the need.
I think this would do the trick:
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
).If you have WordPress bootstrap loaded you can use
get_home_path()
function to get path to the WordPress root directory.I like the accepted answer and @cfx’s solution, but they can be consolidated a bit more:
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.
Why was this made so complicated? One command:
Nginx
grep -i 'root' /etc/nginx/sites-enabled/*
Apache
grep -i 'DocumentRoot' /etc/apache2/sites-enabled/*
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
Try this function for get root directory path: