Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Having some troubles with a PHP script I am trying to build.

I need to access the a file which is located in the root folder of my Wordpress install:


Problem is, my script file is located inside the theme's root folder, and I'm trying to access the root folder when defining a constant.

My script file is located in: root-directory/wp-content/themes/theme-root/

If I use require ABSPATH . 'live-config.php'; it's no good because it looks in the directory where your current file is (ie. theme-root directory instead of my wordpress-root).

I just want to know what the best way would be to grab the root folder of my Wordpress install when defining a constant which is in the theme directory?

In my script, I'm currently trying:

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');

* Define type of server
* Depending on the type other stuff can be configured
* Note: Define them all, don't skip one if other is already defined

define( 'WP_DEV_SERVER', file_exists( ABSPATH . 'dev-config.php' ) );

* Load DB credentials

    require ABSPATH . 'dev-config.php';
    require ABSPATH . 'live-config.php';

But obviously defining ABSPATH in my theme-root does not actually point to the wordpress-root directory.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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:


# Dir structure
├── 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
// 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.

share|improve this answer
Thanks for this kaiser, a very good insight. I will attempt to do something similar for my setup. Certainly solves my issue even though I have to change a few things around. –  remi90 Nov 2 '12 at 12:56

Try taking a look at this answer over at StackOverflow: http://stackoverflow.com/a/2356526/1077363 - it looks similar to the method I used once before but don't have that code to hand right now.

The theory behind it is that you are usually only 2 or 3 directories below the root directory, so checking for the existence of wp-config.php (which is most of the time stored int he root dir) using file_exists() will tell you if you've found the right directory. To get up directories use dirname() (if you nest them like in the other answer you will get there in fewer LOC).

Hope this helps, if you need more help I can dig out my old code later tonight.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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