1

Is it possible to use two different databases on WordPress depending the environment where the WordPress is? Like for example on my testing environment it uses a DataBase that is not Localhost eventhough the testing environment is Localhost, and on my deployed environment it uses a different DataBase without me having to change anything? Whenever I finish working on my LocalHost I upload the files through FTP but I need to change the DataBase information everytime.

3 Answers 3

1

I do a lot of wordpress development (things like themes and plug-ins). I usually install a local copy of wordpress and work off of that. Then once the plug-in or theme is complete, I just export and deploy the specific files to that. There's no need/reason to move all of the files associated with wordpress (including those carrying database information: wp-config.php).

However, in a direct answer to your question, I supposed if that's what you want to do, you could modify the wp-config.php file so that the value of the variables that identify the database depend on your environment. That would be something like this:

///////////////////////////////////////
// Inside wp-config.php
///////////////////////////////////////

// Get as fancy as you need to in determining your environment
if($_SERVER['HTTP_HOST'] == 'localhost') {
    define('DB_NAME', 'my_local_database');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost');
}
else {
    define('DB_NAME', 'my_other_database');
    define('DB_USER', 'username');
    define('DB_PASSWORD', 'password');
    define('DB_HOST', 'path_to_other_host');
}

This might not be a good idea, though, because when you update your wordpress that file could be overwritten. Maybe, who knows. But like I said, you'd be better off just moving the files that are specific to your project (like a custom theme or plugin that you work on on your development environment).

Hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Duh of course. What does the function is_in_localhost() look like thou?
I changed the code so it doesn't use is_in_localhost(). The point of it was that you look up where wp-config is being loaded, then set your variables accordingly.
1

Sure. Set up the database in the wp-config.php file for each environment. See: http://codex.wordpress.org/Editing_wp-config.php#Configure_Database_Settings

Comments

0

You have to find the database configuration file. where it stores all your database information like user name, password. mark this file read only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.