检测WordPress安装



如何检测文件夹是否安装了WordPress ?

WP-CLI这样做并给出错误This does not seem to be a WP installation,并正确检测目录是否有WP安装,即使它位于任何子文件夹(wp-includes/imageswp-includes/js)。

我浏览了代码,它搜索index.php并将内容与原始index.php进行比较。它做的另一件事是检查wp-includes/version.php的存在。得到了这个想法,但它是如何工作的子文件夹像上面提到的仍然不清楚。有人知道怎么做吗?提前谢谢。

查找wp-config.php文件。如果您找到它,require它,然后尝试使用它的常量DB_HOST,DB_USER,DB_PASSWORDDB_NAME连接到与WordPress实例关联的WordPress数据库。如果可以,你很可能有一个工作的WordPress实例。

如果您当前的工作目录没有wp-config.php,则递归地查看父目录,直到(a)找到它或(b)到达顶级目录。

wp-cli做更复杂的事情。但这应该对你有用。

所以我写了一个检测WP安装的脚本。它在wp安装文件夹中工作,但如果它不在WordPress安装文件夹中,它会执行一个无限循环。有人可以指导如何停止在顶级目录,如@O提到的。琼斯吗?这是我的代码。

<?php
function get_wp_index($dir=null) {

if(is_null($dir)){
$dir = getcwd();
}
echo "Currently Looking n";
echo $dir;
$name = $dir.DIRECTORY_SEPARATOR."index.php";
if ( file_exists( $name ) ) {

$index_code = (file_get_contents($name));
if ( preg_match( '|^s*requires*(?s*(.+?)/wp-blog-header.php(['"])|m', $index_code, $matches ) ) {
echo "Is a WP Install";
return;
} else {

echo "Has index File but not one with wp-blog-header";
echo "nn";
//Go one directory up
$up_path = realpath($dir. DIRECTORY_SEPARATOR . '..');
get_wp_index($up_path);
}
} else {
echo 'No Index File Found';
echo "nn";
//Go one directory up
$up_path = realpath($dir. DIRECTORY_SEPARATOR . '..');
echo $up_path;
get_wp_index($up_path);
}
}
get_wp_index();
?>

最新更新