php 如何获取wordpress root url(每页ABSPATH变化)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6175885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:34:59  来源:igfitidea点击:

How to obtain wordpress root url (ABSPATH changes on each page)

phpwordpress

提问by CodeVirtuoso

I have wordpress installed at:

我在以下位置安装了 wordpress:

http://example.com/wordpress/

http://example.com/wordpress/

When on homepage, the ABSPATH constant outputs exactly that, but when you navigate off to some other page, like:

在主页上时, ABSPATH 常量输出完全相同,但是当您导航到其他页面时,例如:

http://example.com/wordpress/contact

http://example.com/wordpress/contact

the ABSPATH also turns to:

ABSPATH 也变成:

http://example.com/wordpress/contact

http://example.com/wordpress/contact

The question is, how can I obtain the actual root (marked in bold) no matter on which page I am - without hard-coding it?

问题是,无论我在哪个页面上,我怎样才能获得实际的根(以粗体标记) - 无需对其进行硬编码?

I'm a bit confused with why ABSPATH changes value, aren't constants unchangeable once they are defined?

我对为什么 ABSPATH 更改值感到有些困惑,常量一旦定义就不是不可更改的吗?

Thanks!

谢谢!

采纳答案by Sujit Agarwal

you can use Site_url();... :)

你可以使用Site_url();... :)

回答by BitTech

I had the same issue on an admin page. Not only do you have to avoid the extra folder that gets inserted, WP may be installed in a folder itself.

我在管理页面上遇到了同样的问题。您不仅要避免插入额外的文件夹,而且 WP 可能会安装在文件夹本身中。

Here is a way, albeit somewhat convoluted and written for clarity, that makes the adjustments for these various items. It does avoid DIRECTORY_SEPARATORissues as well:

这里有一种方法,虽然有些复杂,并且为了清晰起见,可以对这些不同的项目进行调整。它也确实避免了DIRECTORY_SEPARATOR问题:

if (!defined(PLUGINUPDATEMGR_DOMAIN)) 
    define("PLUGINUPDATEMGR_DOMAIN", strtolower( $_SERVER['HTTP_HOST'] ) );

$wprootbase = strtolower( site_url() );

$wprootstart = strpos( $wprootbase, PLUGINUPDATEMGR_DOMAIN ) + 
    strlen( PLUGINUPDATEMGR_DOMAIN  ); // + 1 to strip the leading slash/backslash

$wprootend = strlen( $wprootbase );

$wproot = substr( $wprootbase, $wprootstart, $wprootend );

echo "Local WP path = '" . $wproot . '"';

Shaken, not stirred, output:

摇动,不搅拌,输出:

Local WP path = '/wp/wordpress-3.4.2" 

Of course, YMMV =;?)

当然,YMMV =;?)

回答by Prathik Rajendran M

ABSPATH returns the absolute path to the php file in the server such as /var/www/wordpress/, do check your wordpress install.

ABSPATH 返回服务器中 php 文件的绝对路径,例如 /var/www/wordpress/,请检查您的 wordpress 安装。

The answer to your problem is to use:

您的问题的答案是使用:

site_url()

site_url()

or

或者

bloginfo()

博客信息()

As the people above mentioned.

正如上面提到的人。

You could also use

你也可以使用

$var = get_bloginfo('wpurl');

$var = get_bloginfo('wpurl');

To get the value into a variable

将值放入变量中

回答by Sab

The correct answer is

正确答案是

<?php echo esc_url( home_url( '/' ) ); ?>


Here is some text because I must have at least 30 characters

这是一些文字,因为我必须至少有 30 个字符