問題描述
如何檢索 WordPress CMS 中根目錄的路徑?
最佳解決方案
我猜你需要從外掛或主題中檢測出 WordPress 的根目錄。我在 FireStats 中使用以下程式碼來檢測 FireStats 安裝了 WordPress 外掛的根 WordPress 目錄。
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;
}
次佳解決方案
看看你的 wp-config.php 檔案在 wordpress 根目錄的底部將讓你找到這樣的東西:
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
對於一個示例檔案,請看這裡:http://core.trac.wordpress.org/browser/trunk/wp-config-sample.php
你可以在你的 wordpress 指令碼的其他地方使用這個常數叫做 ABSPATH,在大多數情況下,它應該指向你的 wordpress 根目錄。
第三種解決方案
echo ABSPATH; //這顯示了 WordPress 的絕對路徑
ABSPATH 是在 wp-config.php 檔案中定義的常量。
第四種方案
這是一個老問題,但我有一個新的答案。這一行將返回模板中的路徑::)
$wp_root_path = str_replace('/wp-content/themes', '', get_theme_root());
第五種方案
這個問題有兩個答案 Url& 目錄。無論哪種方式,優雅的方式是定義兩個常量供以後使用。
define (ROOT_URL, get_site_url() );
define (ROOT_DIR, get_theme_root() );
第六種方案
Please try this for get the url of root file.
第一道:
$path = get_home_path();
print "Path: ".$path;
// Return "Path: /var/www/htdocs/" or
// "Path: /var/www/htdocs/wordpress/" if it is subfolder
第二種方式
And you can also use
"ABSPATH"
this constant is define in wordpress config file.
第七種方案
我認為這會做的訣竅:
function get_wp_installation()
{
$full_path = getcwd();
$ar = explode("wp-", $full_path);
return $ar[0];
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。