I am developer of the plugin mapsmarker.com which also offers several APIs which can be accessed directly (eg www.mapsmarker.com/wp-content/plugins/leaflet-maps-marker/leaflet-geojson.php?marker=1) For these APIs I initially wrote the absolut directory path to a file with the following function on plugin install:

file_put_contents(dirname(__FILE__).'/leaflet-wp-path.php', '<?php define(\'WP_PATH\',\''.ABSPATH.'\'); ?>');

In the API-files, the file leaflet-wp-path.php the got included by the following code:

global $wpdb;

I then noticed that on some hosting providers these kind of operation is not supported, causing the plugin install to fail. Therefore I switched to another method for determing the directory-path to wp-config.php:

//info: construct path to wp-config.php with fallback for subdirectory installations
$wp_path = $_SERVER["DOCUMENT_ROOT"]; 
if ( file_exists($wp_path . '/wp-config.php') ) {
} else { 
    $wp_plugin_path_modified = explode(DIRECTORY_SEPARATOR, dirname(__FILE__),-3);
    $wp_path = implode(DIRECTORY_SEPARATOR, $wp_plugin_path_modified);
if ( !file_exists($wp_path . '/wp-config.php') ) {
    echo __('Error: Could not construct path to wp-config.php - please check <a     href="http://mapsmarker.com/path-error">http://mapsmarker.com/path-error</a> for more details.','lmm') . '<br/>Path on your webhost: ' . $wp_path;
} else {

This worked fine even on hosts that don´t allow the function file_put_contents() because the directory path is determined from the current dirname of the API-File.

Now I got a bug report from a user, telling me that this method doesnt work on his host. He writes:

This is the example of the one of icon link. Looks entire plugin links are not correct. Only one thing is working now, it is admin panel configuration. Also it is making markers, but not showing in browsers.

On Windows Web Host http://XXXXX/wordpress/wp-content/plugins/D:/Hosting/5465771/html/wordpress/wp-content/plugins/leaflet-maps-marker/img/logo-mapsmarker.png

On Linux Web Host http://XXXXX/wordpress/wp-content/plugins/D:/inetpub/vhosts/XXXXX/httpdocs/wordpress/wp-content/plugins/leaflet-maps-marker/img/logo-mapsmarker.png

Does anyone know a better method for determing the directory path to wp-config.php to support this kind of hosting configuration?


I came up with this solution.

This function checks in each directory level starting from the directory of the current file for the file wp-config.php.

    function find_wp_config_path() {
        $dir = dirname(__FILE__);
        do {
            if( file_exists($dir."/wp-config.php") ) {
                return $dir;
        } while( $dir = realpath("$dir/..") );
        return null;

To actually include the file you can use this:

if ( ! function_exists('add_action') ) {
    include_once( find_wp_config_path()  . '/wp-load.php' );

This can also be found here: How to determine wordpress base path when wordpress core is not loaded

| improve this answer | |
  • +1 : Thanks, work perfect for my case : a plugin who need access to Wordpress function. – Mike Castro Demaria Mar 24 '14 at 7:59
  • I think $dir = trim(ABSPATH,"/"); would be faster. – Miguel May 30 '14 at 18:10

I guess it sounds silly, but could you work your way up from the plug-in directory?

if ( file_exists('../../../wp-config.php') ) :
    include_once '../../../wp-config.php';

I'm sure someone else will probably have a more elegant solution.

| improve this answer | |
  • I use this approach all of the time. It makes things cleaner, I don't like the idea of relying on external functions to get paths for including files. Have an upvote. – Dwayne Charrington Mar 4 '12 at 22:13
  • I have a more elegant version posted, check it out. – xaedes Dec 20 '12 at 16:24
  • Not a great solution, I run a custom WP installation with a different path for plugins and the theme, and this would not work for me. – Maarten00 Nov 19 '15 at 9:55

it's not my code, found somewhere on stack, but try this:

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;
        include_once $confroot."/wp-load.php";
| improve this answer | |
  • As this is the same solution as the one from bueltge I can only repeat myself: That is a very nasty approach. Output is done with a global variable what is not very nice. The code is really confusing and inefficient. The same can be done in a much better way with my solution I posted. – xaedes Dec 20 '12 at 16:55
  • The entire WordPress platform runs on global variables. Yes, it's not best practice, but it very much fits into how the system is built. – chrisguitarguy Dec 20 '12 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.