use*_*248 4 wordpress root realpath document-root installation-path
有没有办法获得安装WordPress的路径?
我使用以下内容:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
Run Code Online (Sandbox Code Playgroud)
它适用于www.example.com - >/usr/local/pem/vhosts/165312/webspace/httpdocs
这对www.example.com/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";
Run Code Online (Sandbox Code Playgroud)
但它仍然是很复杂的东西.是否有一种简单的方法可以在没有硬编码的情况下安装WordPress(路径)?
注意:1个 WordPress函数将无法工作,因为这是以某种方式在WordPress之外.如上一个例子所述,确定WordPress安装路径的重点是require "wp-blog-header.php";多个WordPress安装,其中每个安装使用不同的文件夹(example.com/blog-one和example.com/blog-two),而不是WordPress MU或多站点.
注意:2如果不是require "$root/$dir/wp-blog-header.php";我使用require "wp-blog-header.php";它,只要文件在同一文件夹中就可以工作,但在我的情况下,文件有时会在不同的文件夹中.
根示例:
下面的示例适用于任何博客(“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.
Run Code Online (Sandbox Code Playgroud)
这使得脚本独立。该脚本适用于任何 WordPress 安装中的任何文件夹,只要该文件夹的数字为 8。如果安装在子文件夹上,则必须增加数字 8。另请记住,当前路径可能有更多或更少的文件夹,这意味着必须相应地调整脚本。
注意:这将通过“硬编码”路径上的文件夹位置来工作,只要所有安装都具有相同的位置,脚本就可以工作。另一种方法是硬编码。
| 归档时间: |
|
| 查看次数: |
19492 次 |
| 最近记录: |