Last Updated: February 25, 2016
·
740
· pimschaaf

Wordpress - Local or live database

Instead of changing wp-config, each time you start working locally or push it live.

    if ("localhost" == $_SERVER['HTTP_HOST']) {
        define('DB_NAME', 'local_databasename');
        define('DB_USER', 'local_password');
        define('DB_PASSWORD', 'local_password');
        define('DB_HOST', 'local_host');
        define('WP_DEBUG', true);
    } else {
        /** MySQL database name */
        define('DB_NAME', 'live_databasename');

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

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

        /** MySQL hostname */
        define('DB_HOST', 'live_host');

        define('WP_DEBUG', false);

        //define('FS_METHOD', 'direct');
}

Props @mikeshawdev

6 Responses
Add your response

If you have multiple environments, you could also use gethostname() and a switch statement to swap between connection details as appropriate

over 1 year ago ·

You could also put other settings such as WP_DEBUG in your switch, so your dev environment will always have debugging turned on and your live one won't

over 1 year ago ·

Cheers for the props :)

over 1 year ago ·

Ah, I see props are working again ;) You're welcome @mikeshawdev

over 1 year ago ·

Another interesting way of storing environment dependent variables, is to set them up as actual environment variables; completely extracting the environment variables from wp-config.php.

Roots.io has comprehensive documentation that could be an improvement to the workflow proposed in this tip. See: http://roots.io/twelve-factor-03-config

over 1 year ago ·

Aye, that looks good that

Just to add another idea to the mix, Mark Jaquith (in his WordPress skeleton: https://github.com/markjaquith/WordPress-Skeleton) has a local-config file. In his wp-config file he does a file_exists check for it which will load in different settings

over 1 year ago ·