What is the best way to get directory path for wp-config.php?

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:

Read More
include_once(dirname($_SERVER['SCRIPT_FILENAME']).'/leaflet-wp-path.php');
include_once(WP_PATH.'wp-config.php');
include_once(WP_PATH.'wp-includes/wp-db.php');
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') ) {
    include_once($wp_path.'/wp-config.php');
    include_once($wp_path.'/wp-includes/wp-db.php');
} else { 
    $wp_plugin_path_modified = explode(DIRECTORY_SEPARATOR, dirname(__FILE__),-3);
    $wp_path = implode(DIRECTORY_SEPARATOR, $wp_plugin_path_modified);
    include_once($wp_path.'/wp-config.php');
    include_once($wp_path.'/wp-includes/wp-db.php');
} 
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?

Related posts

Leave a Reply

5 comments

  1. 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.

    <?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

  2. 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.

  3. For the record, the question asks how to do the wrong thing, even in the time it was asked you should not have used direct access to any of the plugin files, but used probably a uniqly crafted URL parameter like my_plugin_param that its usage will indicate that the plugin is supposed to do whatever is needed. To detect it the plugin should hook on the init action and check if the URL contains my_plugin_param. Once done that way you do not need to bootstrap WordPress as it was already done.

    While wp-config.php should be either in WordPress root, or one directory above it, plugins might be installed outside of the WordPress directories. While not especially popular setup, it can be done.

  4. 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;
            FindWPConfig(dirname(dirname(__FILE__)));
            include_once $confroot."/wp-load.php";
    }
    
  5. The following doesn’t exactly answer what’s being asked in the context of the question, but it does answer exactly what’s in the title of the question.

    For those who are looking for a way to get the path to wp-config.php from within WordPress, the following works fine.

    This is how WP CLI does it:

    Source

    function locate_wp_config() {
        static $path;
    
        if ( null === $path ) {
            $path = false;
    
            if ( getenv( 'WP_CONFIG_PATH' ) && file_exists( getenv( 'WP_CONFIG_PATH' ) ) ) {
                $path = getenv( 'WP_CONFIG_PATH' );
            } elseif ( file_exists( ABSPATH . 'wp-config.php' ) ) {
                $path = ABSPATH . 'wp-config.php';
            } elseif ( file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) {
                $path = dirname( ABSPATH ) . '/wp-config.php';
            }
    
            if ( $path ) {
                $path = realpath( $path );
            }
        }
    
        return $path;
    }