2010-03-01 139 views

回答

25

我猜你需要檢測你的插件或主題的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; 
} 
+0

這是我尋找的...謝謝.. – abhis 2010-03-02 11:45:45

+0

很酷。只要記得重新命名該函數,以便我們在沒有函數名衝突的情況下安裝這兩個插件:) – 2010-03-02 13:12:53

+1

這很好,但是由於OP詢問根目錄,請確保兩個'$ path = dirname(.. .'行不以'。'結尾。「/ wp-config.php」' – 2012-06-12 22:35:45

104

看你的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根目錄。

+8

這隻有在wp-config.php已經包含時纔有用。 在某些情況下(特別是超出上下文ajax調用您的插件代碼),這不適用。 – 2010-03-01 14:43:59

+11

您應該使用內置的WordPress AJAX處理程序來管理所有AJAX調用。 – 2013-02-07 04:56:37

6

這是一個老問題,但我有一個新的答案。這種單一的線將返回路徑的模板中::)

$wp_root_path = str_replace('/wp-content/themes', '', get_theme_root()); 
+1

假定模板是直到wp-content,可能並非如此。您可以在安裝WP時重新定義內容目錄的名稱。一般不推薦使用這種插件,因爲很多插件都會做出相同的假設和突破,但可能性存在。但是,在這裏繼續重複這些不正確的假設是沒有意義的。 – Jason 2013-02-05 13:57:50

+0

老實說,我不知道wp-content可以重命名。在這種情況下,您可以將我的代碼段中的wp-content更改爲修改的名稱,它仍然可以工作。 – yitwail 2013-02-14 18:21:44

+0

@yitwall你做了一個巨大的失敗。如果wp-content被重新命名,你不會知道它是一個插件還是主題開發者,你無法確切地詢問每個潛在用戶的wp-content名稱,然後將其添加到代碼庫中。衛生署! – 2015-07-03 12:50:06

25

呼應ABSPATH; //這說明WordPress的

ABSPATH的絕對路徑是在wp-config.php文件中定義的常量。

+0

這應該是選定的答案.. – 2016-04-24 07:46:22

4

有此問題的網址&目錄2個回答。無論哪種方式,優雅的方式是定義兩個常量供以後使用。

define (ROOT_URL, get_site_url()); 
define (ROOT_DIR, get_theme_root()); 
2

我認爲這會做的伎倆:

function get_wp_installation() 
{ 
    $full_path = getcwd(); 
    $ar = explode("wp-", $full_path); 
    return $ar[0]; 
} 
+0

這工作。感謝名單 – 2015-04-28 11:42:37

3
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. 
0

如果您已加載WordPress引導程序,則可以使用get_home_path()函數獲取WordPress根目錄的路徑。

1

要檢索路徑,您可以使用功能<?php $path = get_home_path(); ?>。我不想重複這裏已經說過的內容,但我想添加一件事情,但我想添加一件事:

如果您使用Windows服務器,這是WordPress安裝的罕見情況,但有時還會發生,您可能會面對路徑輸出存在問題。它可能會錯過某處的「\」,如果您將使用這樣的路徑,則會出現錯誤。所以輸出的時候一定要消毒路徑:

<?php 

$path = get_home_path(); 
$path = wp_normalize_path ($path); 

// now $path is ready to be used :) 

?> 
0

主題的根目錄路徑代碼

<?php $root_path = get_home_path(); ?> 
print "Path: ".$root_path; 

返回 「路徑:/無功/網絡/ htdocs中/」 或「路徑: /無功/網絡/ htdocs中/ WordPress的/」如果是子文件夾

主題根路徑

$theme_root = get_theme_root(); 
echo $theme_root 

結果: -/home/user中/的public_html /可溼性粉劑內容/主題

0

試試這個功能獲得root目錄路徑:

get_template_directory_uri();