wordpress 获取WordPress安装文件夹路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10942197/
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
Get WordPress installation folder path
提问by user983248
Is there a way to get the path to where WordPress is installed?
有没有办法获得安装WordPress的路径?
I was using the following:
我正在使用以下内容:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
It is fine for www.example.com -> /usr/local/pem/vhosts/165312/webspace/httpdocs
www.example.com -> /usr/local/pem/vhosts/165312/webspace/httpdocs 没问题
It is not fine for www.example.com/blog since I need to hard code the folder name (blog).
www.example.com/blog 不好,因为我需要对文件夹名称(博客)进行硬编码。
Later I found a way by using this:
后来我通过使用这个找到了一种方法:
$iroot = getcwd();
$folder = explode("/", $iroot);
$dir = $folder[8]; // I know is 8
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require "$root/$dir/wp-blog-header.php";
But still it is a lot of complicated stuff. Is there a simple way to get where WordPress is installed (the path) without hard coding?
但它仍然是很多复杂的东西。有没有一种简单的方法可以在没有硬编码的情况下获得 WordPress 的安装位置(路径)?
Note: 1WordPress functions will not work since this is somehow outside WordPress. As noted on the last example, the whole point of determine the WordPress installation path is to require "wp-blog-header.php";
on multiple WordPress installations where each installation uses a different folder (example.com/blog-one and example.com/blog-two), not WordPress MU or multi site.
注意:1WordPress 功能将无法工作,因为这在 WordPress 之外。如上一个示例所述,确定 WordPress 安装路径的重点在于require "wp-blog-header.php";
多个 WordPress 安装,其中每个安装使用不同的文件夹(example.com/blog-one 和 example.com/blog-two),而不是 WordPress MU或多站点。
Note: 2If instead of require "$root/$dir/wp-blog-header.php";
I use require "wp-blog-header.php";
it will work as long as the file is in the same folder, but in my case the file will be sometimes in a different folder.
注意:2如果不是require "$root/$dir/wp-blog-header.php";
我使用require "wp-blog-header.php";
它,只要文件在同一个文件夹中,它就会工作,但在我的情况下,文件有时会在不同的文件夹中。
采纳答案by user983248
Root example:
根示例:
- /usr/local/pem/vhosts/165312/webspace/httpdocs/blog-one
- /usr/local/pem/vhosts/165312/webspace/httpdocs/blog-two
- /usr/local/pem/vhosts/165312/webspace/httpdocs/some-blog
- /usr/local/pem/vhosts/165312/webspace/httpdocs/blog-one
- /usr/local/pem/vhosts/165312/webspace/httpdocs/blog-two
- /usr/local/pem/vhosts/165312/webspace/httpdocs/some-blog
The example below will work on anyof the blogs ("blog-one", "blog-two" and "some-blog") and the script or file can be installed on anysubfolder of the blog.
下面的示例适用于任何博客(“blog-one”、“blog-two”和“some-blog”),并且脚本或文件可以安装在博客的任何子文件夹中。
$current_path = getcwd(); // Get the current path to where the file is located
$folder = explode("/", $current_path); // Divide the path in parts (aka folders)
$blog = $folder[8]; // The blog's folder is the number 8 on the path
// $root = path without the blog installation folder.
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
// Now I can require any WordPress file
require "$root/$dir/wp-blog-header.php";
// For the current installation
// For example, wp-blog-header.php to get the blog name or
// wp-config.php to access the database.
This makes the script independent. The script will work on any folder on any WordPress installation installation as long as the folder is the number 8. If the installation is on a sub folder, the number 8 will have to be increased. Also bear in mind that the current path may have more or fewer folders, meaning that the script has to be adapted accordingly.
这使得脚本独立。只要文件夹是数字 8,脚本就可以在任何 WordPress 安装安装上的任何文件夹上工作。如果安装在子文件夹上,则必须增加数字 8。还要记住,当前路径可能有更多或更少的文件夹,这意味着必须相应地调整脚本。
Note: This will work by "hard coding" the folder position on the path and, as long as all installation have the same position, the script will work. The other way around is to hard code.
注意:这将通过“硬编码”路径上的文件夹位置来工作,只要所有安装都具有相同的位置,脚本就可以工作。另一种方法是硬编码。
回答by Estev?o Lucas
Use the ABSPATH constant:
使用 ABSPATH 常量:
<?php echo ABSPATH ?>
This should print your WordPress path.
这应该打印您的 WordPress 路径。
回答by akamaozu
Here's what I'm doing:
这是我在做什么:
Setup:
设置:
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$scriptName = $_SERVER['SCRIPT_NAME'];
$queryArray = explode("/", $scriptName);
$queryLength = count($queryArray);
Usage:
用法:
require_once($docRoot . ($queryLength > 2 ? "/".$queryArray[$queryLength - 2] : "" ) . '/wp-blog-header.php');
This is working very well for me at the moment for instantiating a WordPress environment regardless of where the script is. Definitely need to sanitize before using any $_SERVERvariables (and I need to triplecheck this outside my localhost environment), but I don't think it requires severe modifications to be a practical universal solution.
目前,这对我来说非常有效,无论脚本在哪里,都可以实例化 WordPress 环境。在使用任何$_SERVER变量之前绝对需要清理(并且我需要在本地主机环境之外对其进行三重检查),但我认为它不需要进行重大修改即可成为实用的通用解决方案。
回答by Drahkar
If all you are trying for is the current directory of where the file being called is located, then I would use:
如果您想要的只是被调用文件所在的当前目录,那么我将使用:
realpath(dirname(__FILE__))
A lot of this depends on where the file is located. If it is in the same directory as the WordPress installation then it would be something like:
这在很大程度上取决于文件所在的位置。如果它与 WordPress 安装在同一目录中,那么它将类似于:
<?php
$root = realpath(dirname(__FILE__));
require "$root/wp-blog-header.php";
?>
回答by FatalError
If you can reach the database, you need to do
如果可以访问数据库,则需要执行
update_option('wp_dir',dirname(__FILE__));
in file wp-blog-header.php.
在文件 wp-blog-header.php 中。
Then connect to the database from any application and retrieve the path with the following query:
然后从任何应用程序连接到数据库并使用以下查询检索路径:
select option_value from wp_options where option_name ='wp_dir';
Of course, you would need to know the database name and WordPress table prefix.
当然,您需要知道数据库名称和 WordPress 表前缀。
PS: This may or may not be your solution, but it is one of the ways.
PS:这可能是也可能不是您的解决方案,但它是方法之一。
回答by William Turrell
This is how to do it from within WordPress(which isn't what the questioner was asking for, but what I suspect many people will be searching for…)
这是如何在 WordPress 中做到这一点(这不是提问者所要求的,但我怀疑很多人会搜索......)
There's a dedicated function, get_home_path(), which'll give you something like:
有一个专用函数get_home_path(),它会给你类似的东西:
/srv/www/my-site/htdocs/
Other functions for file and content directories(and web paths).
回答by Sahib Khan
$WP_PATH = implode("/", (explode("/", $_SERVER["PHP_SELF"], -4)));
Will give you the exact WordPress root.
将为您提供确切的 WordPress 根。
For example, your root address is www.myroot.com, and your WordPress site is in the WordPress folder in public_html, then wwww.myroot.com/wordpress/ will be the result in $WP_PATH. In such a case your current file name should be in your plugin root directory. Like some_plugin is my plugin name. So the full path will become:
例如,您的根地址是 www.myroot.com,而您的 WordPress 站点在 public_html 中的 WordPress 文件夹中,那么 wwww.myroot.com/wordpress/ 将是 $WP_PATH 中的结果。在这种情况下,您当前的文件名应该在您的插件根目录中。就像 some_plugin 是我的插件名称。所以完整路径将变为:
localhot/wordpress/wp_content/plugins/some_plugin/
And localhot/wordpress/wp_content/plugins/some_plugin/myfile.php
is my file name where I wrote this script, will give me /wordpress
. This worked for me as I wanted my wordpress directory name. So look for contents like this:
而且localhot/wordpress/wp_content/plugins/some_plugin/myfile.php
是我写这个剧本我的文件名,就会给我/wordpress
。这对我有用,因为我想要我的 wordpress 目录名称。所以寻找这样的内容:
$WP_PATH = implode("/", (explode("/", $_SERVER["PHP_SELF"], -4)));
require_once( $_SERVER['DOCUMENT_ROOT'].$WP_PATH '/wp-load.php');
回答by user787348
Why not simply do the following in the wp-config.php:
为什么不简单地在 wp-config.php 中执行以下操作:
$WP_PATH = implode("/", (explode("/", $_SERVER["PHP_SELF"], -1)));
e.g.
例如
$WP_PATH = implode("/", (explode("/", "/one/two/three/index.php", -1)));
will return "/one/two/three" and
将返回 "/one/two/three" 和
$WP_PATH = implode("/", (explode("/", "/index.php", -1)));
will return ""
将返回 ””