logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$30
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 http://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:

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?

This question has been answered.

Robert Harm | 01/11/12 at 1:13am Edit


(5) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/11/12
    1:17am
    Fahd Murtaza says:

    Wow, a nice question. Interesting as have been into such situations. I have successfully made some plugins work on windows and Linux systems without worrying about such issues. I will get back to you in an hour.

    • 01/11/12 1:29am

      Fahd Murtaza says:

      Did you try something like


      <?php
      $path = getcwd();
      echo $path;
      ?>


      If you call this in your plugin, you can take out the "/wp-content/plugins/xyz-plugin/ " by splitting in with "/"

      That should do it.

  • avatar
    Last edited:
    01/11/12
    1:20am
    Arnav Joy says:

    try this

    $root = dirname(dirname(dirname(dirname(__FILE__))));

    require_once($root.'/wp-config.php');

  • avatar
    Last edited:
    01/11/12
    1:33am
    Buzu B says:

    This is how I do it:

    $path = split('wp-content', __FILE__);
    $path = $path[0];
    include($path .'wp-load.php');


  • avatar
    Last edited:
    01/11/12
    2:13am
    Julio Potier says:

    Hello

    I code more than 30 plugins and my code always works :

    while(!is_file('wp-config.php')){
    if(is_dir('../')) chdir('../');
    else die('Could not find WordPress.');
    }
    include( 'wp-config.php' );


    See you soon ;)

  • avatar
    Last edited:
    01/11/12
    2:38am
    Romel Apuya says:

    add this


    // let's make sure the $_SERVER['DOCUMENT_ROOT'] variable is set
    if(!isset($_SERVER['DOCUMENT_ROOT'])){ if(isset($_SERVER['SCRIPT_FILENAME'])){
    $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF'])));
    }; };
    if(!isset($_SERVER['DOCUMENT_ROOT'])){ if(isset($_SERVER['PATH_TRANSLATED'])){
    $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0-strlen($_SERVER['PHP_SELF'])));
    }; };
    // $_SERVER['DOCUMENT_ROOT'] is now set - you can use it as usual...




    before

    $wp_path = $_SERVER["DOCUMENT_ROOT"];

This question has expired.



Christianto, Robert Harm voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.