Retrieve WordPress root directory path?

2020-01-26 06:01发布

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

标签: wordpress
12条回答
Root(大扎)
2楼-- · 2020-01-26 06:49

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

查看更多
forever°为你锁心
3楼-- · 2020-01-26 06:52

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;
}
查看更多
我命由我不由天
4楼-- · 2020-01-26 06:52

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.

查看更多
beautiful°
5楼-- · 2020-01-26 06:55
   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.
查看更多
再贱就再见
6楼-- · 2020-01-26 06:56

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

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

查看更多
劫难
7楼-- · 2020-01-26 06:58

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

?>
查看更多
登录 后发表回答