Last Updated: February 25, 2016
·
772
· 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 ·