I am trying to setup 3 environments: Localhost, Staging and Development. The issue is that each environment has its own db.

We want to have ONE config file which will dynamically change the Db connection.

Example: Dev Db = define('DB_NAME', 'dev.domainname.com'); Staging Db = define('DB_NAME', 'staging.domainname.com'); Production Db = define('DB_NAME', 'domainname.com');

In the WP-CONFIG, I was going to add the following function to do the conditional as:

function SetEnvironment()
        $envname = '';

        if ( stristr( $_SERVER['SERVER_NAME'], "localhost" ) ) {     $envname = 'dev.'    }

        if ( stristr( $_SERVER['SERVER_NAME'], "staging" ) ) {    $envname = 'staging.'    }

        return $envname;

In the WP-CONFIG set:

    /** The name of the database for WordPress */
define('DB_NAME', SetEnvironment() + 'domainname.com');

/** MySQL database username */
define('DB_USER', 'someusernamehere');

/** MySQL database password */
define('DB_PASSWORD', 'somepasswordhere');

/** MySQL hostname */
define('DB_HOST', SetEnvironment() + 'domainname.com');

Is there a better way of doing a dynamic settings in WP-CONFIG?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.