有办法获得安装WordPress的路径吗?
我使用了以下方法:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
对/usr/local/pem/vhosts/165312/webspace/httpdocs www.example.com ->来说没问题
对于www.example.com/blog来说不是很好,因为我需要对文件夹名(blog)进行硬编码。
后来,我通过使用以下方法找到了一种方法:
$iroot = getcwd();
$folder = explode("/", $iroot);
$dir = $folder[8]; // I know is 8
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require "$root/$dir/wp-blog-header.php";
但它仍然是很多复杂的东西。有没有一种简单的方法可以在没有硬编码的情况下获得安装WordPress的位置(路径)?
注意: 1 WordPress函数将无法工作,因为这在WordPress之外。正如上一个例子所指出的,确定WordPress安装路径的全部要点是在多个WordPress安装上使用require "wp-blog-header.php";
,其中每个安装都使用不同的文件夹(example.com/blog-one和example.com/blog-2),而不是WordPress MU或多站点。
备注: 2,如果我使用的不是require "$root/$dir/wp-blog-header.php";
,而是使用require "wp-blog-header.php";
,只要文件位于同一个文件夹中,它就能工作,但在我的例子中,文件有时会位于不同的文件夹中。
发布于 2012-06-13 15:38:16
根示例:
下面的示例将适用于博客的任何(“blog -1”、“blog-2”和“blog-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.
这使得脚本独立。只要该文件夹是8号,脚本将在任何WordPress安装的任何文件夹上工作。如果安装在子文件夹上,则必须增加数字8。还要记住,当前路径可能有更多或更少的文件夹,这意味着脚本必须相应地进行调整。
Note:这将通过“硬编码”路径上的文件夹位置来工作,只要所有安装都有相同的位置,脚本就能工作。相反的是硬代码。
发布于 2012-06-08 22:17:54
使用ABSPATH常量:
<?php echo ABSPATH ?>
这将打印您的WordPress路径。
发布于 2012-08-09 20:39:16
我正在做的事情是:
设置
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$scriptName = $_SERVER['SCRIPT_NAME'];
$queryArray = explode("/", $scriptName);
$queryLength = count($queryArray);
使用
require_once($docRoot . ($queryLength > 2 ? "/".$queryArray[$queryLength - 2] : "" ) . '/wp-blog-header.php');
对于我来说,这对于实例化WordPress环境非常有效,不管脚本在哪里。在使用任何$_SERVER变量之前,肯定需要对其进行消毒(我需要在本地主机环境之外对此进行检查),但我不认为需要进行严格的修改才能成为实用的通用解决方案。
https://stackoverflow.com/questions/10942197
复制相似问题