问题
How to have access to wp-load.php ?
I can access it using the following
require_once("../../../../wp-load.php");
But I need to find it dynamically so I am using the following but none of them works.
require_once( dirname(__FILE__).'/../../../wp-load.php');
require_once( dirname(__FILE__)."/../../../wp-load.php");
require_once( ABSPATH.'wp-load.php');
require_once( ABSPATH."wp-load.php");
How to have access to localhost:8888/wordpress/wp-load.php?
回答1:
Add below code snippets at beginning of file where you require to load wp-load.php
/* FindWPConfig - searching for a root of wp */
function FindWPConfig($dirrectory){
global $confroot;
foreach(glob($dirrectory."/*") as $f){
if (basename($f) == 'wp-config.php' ){
$confroot = str_replace("\\", "/", dirname($f));
return true;
}
if (is_dir($f)){
$newdir = dirname(dirname($f));
}
}
if (isset($newdir) && $newdir != $dirrectory){
if (FindWPConfig($newdir)){
return false;
}
}
return false;
}
if (!isset($table_prefix)){
global $confroot;
FindWPConfig(dirname(dirname(__FILE__)));
include_once $confroot."/wp-config.php";
include_once $confroot."/wp-load.php";
}
回答2:
The same question is here.
Denzel Chia answer was :
Put the following in the beginning of your file that requires wp-load.php
//include wp-config or wp-load.php
$root = dirname(dirname(dirname(dirname(__FILE__))));
if (file_exists($root.'/wp-load.php')) {
// WP 2.6
require_once($root.'/wp-load.php');
} else {
// Before 2.6
require_once($root.'/wp-config.php');
}
Hope it helps
来源:https://stackoverflow.com/questions/16894946/cant-access-wp-load-php-dynamically