2010-03-01 141 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();