This is probably the most comprehensive (convoluted) way to load wp-config.php and wp-load.php I’ve ever encountered. There is no reason (except in rare cases) a plugin should be loading these files.



Usually plugins (or themes) will load these core files because they’re trying to access WordPress core functions that are unavailable to them, but this usually only happens when the code that is needing these functions is loaded improperly in the first place. This is often done with forms; the action attribute will be set to the URL of a PHP “processing” file, and inside of that file the developer wants access to core WordPress functions and so must load wp-load.php.

If you are processing a form, it is much better to process it via a function that is connected to the “init” (or similar) hook. By doing that, you completely alleviate the need to load these files., and you get rid of some really crappy code.

/* if(file_exists('../../../wp-config.php')) { require_once("../../../wp-config.php"); } else if(file_exists('../../wp-config.php')) { require_once("../../wp-config.php"); } else if(file_exists('../wp-config.php')) { require_once("../wp-config.php"); } else if(file_exists('wp-config.php')) { require_once("wp-config.php"); } else if(file_exists('../../../../wp-config.php')) { require_once("../../../../wp-config.php"); } else if(file_exists('../../../../wp-config.php')) { require_once("../../../../wp-config.php"); } else { echo '<p>Cannnot find wp-config.php. Maybe a config error with "custom download url" setting.</p>'; exit; } */ if(file_exists('../../../wp-load.php')) { require_once("../../../wp-load.php"); } else if(file_exists('../../wp-load.php')) { require_once("../../wp-load.php"); } else if(file_exists('../wp-load.php')) { require_once("../wp-load.php"); } else if(file_exists('wp-load.php')) { require_once("wp-load.php"); } else if(file_exists('../../../../wp-load.php')) { require_once("../../../../wp-load.php"); } else if(file_exists('../../../../wp-load.php')) { require_once("../../../../wp-load.php"); } else { if(file_exists('../../../wp-config.php')) { require_once("../../../wp-config.php"); } else if(file_exists('../../wp-config.php')) { require_once("../../wp-config.php"); } else if(file_exists('../wp-config.php')) { require_once("../wp-config.php"); } else if(file_exists('wp-config.php')) { require_once("wp-config.php"); } else if(file_exists('../../../../wp-config.php')) { require_once("../../../../wp-config.php"); } else if(file_exists('../../../../wp-config.php')) { require_once("../../../../wp-config.php"); } else { exit; } }

Submitted by Chris McCoy.