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:
wordpress-root/live-config.php
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
*/
if ( WP_DEV_SERVER )
require ABSPATH . 'dev-config.php';
else
require ABSPATH . 'live-config.php';
But obviously defining ABSPATH in my theme-root does not actually point to the wordpress-root directory.
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
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
Explanation and drawbacks
DS
is just a short constant made to wrapDIRECTORY_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 theSERVER_NAME
or theHTTP_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 namedconfig
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.
Try taking a look at this answer over at StackOverflow: https://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) usingfile_exists()
will tell you if you’ve found the right directory. To get up directories usedirname()
(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.