服务器wp根目录文件,检索WordPress根目录路径?

检索WordPress根目录路径?

如何在WordPress CMS中检索根目录的路径?

Aadi asked 2020-02-03T21:19:38Z

12个解决方案

120 votes

在wordpress根目录中的wp-config.php文件的底部,您会发现类似以下内容:

if ( !defined('ABSPATH') )

define('ABSPATH', dirname(__FILE__) . '/');

有关示例文件,请在此处查看:

[http://core.trac.wordpress.org/browser/trunk/wp-config-sample.php]

您可以在wordpress脚本的其他位置使用称为ABSPATH的常量,并且在大多数情况下,它应指向wordpress根目录。

stefanglase answered 2020-02-03T21:20:30Z

41 votes

echo ABSPATH //这显示了WordPress的绝对路径

ABSPATH是wp-config.php文件中定义的常数。

Jaya Kuma answered 2020-02-03T21:20:54Z

24 votes

注意:此答案确实很老,此后WordPress领域可能发生了变化。

我猜想您需要从插件或主题中检测WordPress根目录。我在FireStats中使用以下代码来检测安装了WordPress插件的FireStats的根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;

}

Omry Yadan answered 2020-02-03T21:19:56Z

7 votes

这是一个老问题,但是我有一个新答案。 此行将返回模板内的路径::)

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

yitwail answered 2020-02-03T21:21:14Z

5 votes

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.

Ritesh d joshi answered 2020-02-03T21:21:38Z

5 votes

这个问题的网址和目录有2个答案。 无论哪种方式,优雅的方式都是定义两个常量供以后使用。

define (ROOT_URL, get_site_url() );

define (ROOT_DIR, get_theme_root() );

Naty answered 2020-02-03T21:21:58Z

2 votes

我认为这可以解决问题:

function get_wp_installation()

{

$full_path = getcwd();

$ar = explode("wp-", $full_path);

return $ar[0];

}

codingpuss answered 2020-02-03T21:22:18Z

2 votes

要检索路径,可以使用函数<?php $path = get_home_path(); ?>。我不想重复这里已经说过的内容,但是我想再添加一件事:

如果您使用的是Windows服务器,这在WordPress安装中很少见,但有时仍会发生,那么路径输出可能会遇到问题。 它可能会在某处遗漏“ \”,并且如果使用这样的路径,则会出现错误。 因此,在输出时,请确保清理路径:

$path = get_home_path();

$path = wp_normalize_path ($path);

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

?>

Nick Surmanidze answered 2020-02-03T21:22:43Z

2 votes

尝试使用此功能获取根目录路径:

get_template_directory_uri();

Heena Patel answered 2020-02-03T21:23:03Z

1 votes

主题根目录路径代码

print "Path: ".$root_path;

返回“路径:/ var / www / htdocs /”或“路径:   / var / www / htdocs / wordpress /”(如果它是子文件夹)

主题根路径

$theme_root = get_theme_root();

echo $theme_root

结果:-/ home / user / public_html / wp-content / themes

Vivek Tamrakar answered 2020-02-03T21:23:36Z

0 votes

您可以使用get_site_url()函数来获取wordpress网站的基本URL。

有关更多信息,请访问[http://codex.wordpress.org/Function_Reference/get_site_url]

Arun Vasudevan Nair answered 2020-02-03T21:24:01Z

0 votes

如果您已加载WordPress引导程序,则可以使用get_home_path()函数获取WordPress根目录的路径。

Sisir answered 2020-02-03T21:24:21Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值