wordpress 检索WordPress根目录路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2354633/
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-09-08 18:35:15  来源:igfitidea点击:

Retrieve WordPress root directory path?

wordpress

提问by Aadi

How can I retrieve the path to the root directory in WordPress CMS?

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

采纳答案by Omry Yadan

Note: This answer is really old and things may have changed in WordPress land since.

注意:这个答案真的很旧,从那以后,WordPress 领域的情况可能已经发生了变化。

I am guessing that you need to detect the WordPress root from your plugin or theme. I use the following code in FireStatsto detect the root WordPress directory where FireStats is installed a a WordPress plugin.

我猜你需要从你的插件或主题中检测 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;
}

回答by stefanglase

Looking at the bottom of your wp-config.php file in the wordpress root directory will let you find something like this:

查看 wordpress 根目录中 wp-config.php 文件的底部,您会发现如下内容:

if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

For an example file have a look here:
http://core.trac.wordpress.org/browser/trunk/wp-config-sample.php

有关示例文件,请查看此处:http:
//core.trac.wordpress.org/browser/trunk/wp-config-sample.php

You can make use of this constant called ABSPATH in other places of your wordpress scripts and in most cases it should point to your wordpress root directory.

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

回答by Jaya Kuma

echo ABSPATH;// This shows the absolute path of WordPress

echo ABSPATH;// 显示WordPress的绝对路径

ABSPATHis a constant defined in the wp-config.php file.

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

回答by yitwail

This an old question, but I have a new answer. This single line will return the path inside a template: :)

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

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

回答by Naty

There are 2 answers for this question Url & directory. Either way, the elegant way would be to define two constants for later use.

这个问题有 2 个答案 Url & directory。无论哪种方式,优雅的方法是定义两个常量供以后使用。

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

回答by Ritesh d joshi

   Please try this for get the url of root file.

First Way:

第一种方式:

 $path = get_home_path();
   print "Path: ".$path; 
// Return "Path: /var/www/htdocs/" or

// "Path: /var/www/htdocs/wordpress/" if it is subfolder

Second Way:

第二种方式:

And you can also use 

    "ABSPATH"

this constant is define in wordpress config file.

回答by Nick Surmanidze

For retrieving the path you can use a function <?php $path = get_home_path(); ?>. I do not want to just repeat what had been already said here, but I want to add one more thing:

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

If you are using windows server, which is rare case for WordPress installation, but still happens sometimes, you might face a problem with the path output. It might miss a "\" somewhere and you will get an error if you will be using such a path. So when outputting make sure to sanitize the path:

如果您使用的是 windows server,这对于 WordPress 安装来说是罕见的情况,但有时仍然会发生,您可能会遇到路径输出的问题。它可能会在某处遗漏一个“\”,如果您将使用这样的路径,您将收到错误消息。因此,在输出时确保清理路径:

<?php 

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

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

?>

回答by Vivek Tamrakar

theme root directory path code

主题根目录路径代码

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

Return "Path: /var/www/htdocs/" or "Path: /var/www/htdocs/wordpress/" if it is subfolder

Theme Root Path

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

主题根路径

 $theme_root = get_theme_root();
 echo $theme_root

Results:- /home/user/public_html/wp-content/themes

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

回答by codingpuss

I think this would do the trick:

我认为这可以解决问题:

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

回答by cfx

I like @Omry Yadan's solution but I think it can be improved upon to use a loop in case you want to continue traversing up the directory tree until you find where wp-config.phpactually lives. Of course, if you don't find it and end up in the server's root then all is lost and we return a sane value (false).

我喜欢@Omry Yadan 的解决方案,但我认为可以改进使用循环,以防您想继续遍历目录树直到找到wp-config.php实际所在的位置。当然,如果你没有找到它并最终在服务器的根目录中,那么一切都将丢失,我们将返回一个合理的值 ( false)。

function wp_get_web_root() {

  $base = dirname(__FILE__);
  $path = false;

  while(!$path && '/' != $base) {
    if(@file_exists(dirname($base)).'/wp-config.php') {
      $path = dirname($base);
    } else {
      $base = dirname($base);
    }
  }

  return $path;
}