Retrieve WordPress root directory path?

后端 未结 13 1443
闹比i
闹比i 2020-11-28 07:48

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

13条回答
  •  臣服心动
    2020-11-28 08:35

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

提交回复
热议问题