0

我有一个在 Wordpress 范围之外执行的 php 脚本,但是我需要使用在wp-config.php. 我想让脚本平台独立。

我的脚本位于子文件夹(一个主题)中,我正在尝试迭代以找到 wp-config。我的主题文件最好可以放在不同的deps(子文件夹)中!

我无法在我的 while 循环中获得运行一圈的语句,我不明白为什么!我正在尝试设置条件,使其在系统根目录中停止,如 (c:)

function getWPConfig() {
    $dir = dirname(__FILE__);
    $dir = addslashes($dir);    
    while ($pos = strpos($dir, "\\") !== false) { //&& strpos($dir, (string)DIRECTORY_SEPARATOR) > 1
        $wpConfig = $dir . DIRECTORY_SEPARATOR .'wp-config.php';
        if (file_exists($wpConfig)) {
            echo 'Found wp-config: '. $wpConfig;
            return $wpConfig;
        } else {
            'Could not find: '. $dir . DIRECTORY_SEPARATOR .'wp-config.php';
        }
        $tempDir = explode(DIRECTORY_SEPARATOR, $dir);
        $end = end($tempDir);
        $dir = str_replace($end, '', $tempDir);
    }
    return;
}
4

1 回答 1

1

在@Pekka 的建议之后,我改写了这个函数!:-)

function getWPConfig() {
    $dir = dirname(__FILE__);
    $lastDir = __FILE__;
    while (strlen($dir) < strlen($lastDir)) {
        $wpConfig = $dir . DIRECTORY_SEPARATOR .'wp-config.php';
        if (file_exists($wpConfig)) {   
            return $wpConfig;
        }
        $lastDir = $dir;
        $dir = dirname($dir);
    }
    return;
}
于 2013-04-22T08:23:25.327 回答