Accessing site’s root from themes folder

I guess you’re talking about a site specific wp-config.php file that resides in your themes folder.

At the point where you’re loading the wp-config.php file, WP isn’t fully loaded, so you ain’t got any constants or filesystem API or other basic API functions available.

Here’s how I approach this:

Structure

# Dir structure
~/ROOT
├── config
├── wp-content
│   ├── themes
│   └── plugins
│   └── mu-plugins

Inside the config folder, I got all config files that are site specific. To identify them easily, they’re named after the domains they’re used on.

The wp-config.php

# Config file suffix
! empty( $_SERVER['SERVER_NAME'] ) AND $suffix = $_SERVER['SERVER_NAME'];
! isset( $suffix ) AND ! empty( $_SERVER['HTTP_HOST'] ) AND $suffix = $_SERVER['HTTP_HOST'];

# CONFIG FILE PATH: Sub of root ~/config
$config_path = dirname( __FILE__ ).DS.'config';

// inside wp-config.php
# LOAD CONFIG FILE
// local
if ( file_exists( "{$config_path}/_local.php" ) )  
{
    require( "{$config_path}/_local.php" );
}
// Stage
elseif ( file_exists( "{$config_path}/{$suffix}-stage.php" ) ) 
{
    require( "{$config_path}/{$suffix}-stage.php" );
}
// Production
elseif ( file_exists( "{$config_path}/{$suffix}.php" ) ) 
{
    require( "{$config_path}/{$suffix}.php" );
}
unset( $suffix, $config_path );

Explanation and drawbacks

DS is just a short constant made to wrap DIRECTORY_SEPARATOR. As I use it in lots of places and like to hold my lines short, I set it.

So the $suffix is what I retrieve from the SERVER_NAME or the HTTP_HOST. The point is that you can’t be sure which one is set. Therefore I’m testing both.

The $config_path simply is the path of the current file + a folder named config on level below it.

The files themselves are named example.com.php. So I can find them easily by just identifying the domain and I’m done. This helps keeping things clean.

The first check that I do is for a _local.php file which holds my local configuration. I skip that step by deleting those lines for files I use on servers. It’s just there to let my local setup run.

The second check is for the stage file. This will also get deleted on production sites.

I hope this helps other people avoiding things like the setup shown here.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)