Closed bkjproductions closed 5 years ago
@bkjproductions Can you share what the exact error is?
@schlessera I figured it out, had a problem using plugin_dir_url instead of plugin_dir_path() I saw no errors, but then I realized I should look for debug.log as well as the error log in wp-admin. Thanks for asking me to be more precise about the errors!
Good news is this thing works great! I needed a way to toggle the database name, and this does the trick. If anyone else wants to try something like this, create some sort of way for the user to choose a database, and put their choice into a GET parameter, perhaps "dbswap_switch". Then:
function dbswap_switch_check() {
if ( @$_GET['dbswap_switch'] == 'shadow' ) {
$dbname = 'mydb_shadow';
// edit wp-config!
dbswap_edit_wp_config($dbname);
}
if ( @$_GET['dbswap_switch'] == 'live' ) {
$dbname = 'mydb_live';
dbswap_edit_wp_config($dbname);
}
}
function dbswap_edit_wp_config($dbname) {
include(plugin_dir_path(__FILE__) . 'WPConfigTransformer.php');
// instantiate
$config_transformer = new WPConfigTransformer( ABSPATH . 'wp-config.php' );
//Edit constants
$config_transformer->update( 'constant', 'DB_NAME', $dbname, array( 'raw' => true) );
}
In my situation because we're testing we have two databases with the same username/pw.
@bkjproductions I'm glad you got it to work!
Absolutely make sure you add proper security measures around the above piece of code, though, you don't want random external visitors to be able to mess with your database.
I'm closing this issue now. If you happen to need more assistance with using the wp-cli/wp-config-transformer
package, please join us in the official #cli channel in the MakeWP Slack team.
Sorry if this is not the right place to ask this question!
I am writing a plugin that will switch databases, so I need to rewrite the three DB-related constants. This wp-config editor seems like just the ticket for it!
I was thinking I could just add this bit of code in:
include(plugin_dir_url(__FILE__) . 'WPConfigTransformer.php')
;$config_transformer = new WPConfigTransformer( ABSPATH . 'wp-config.php' );
$config_transformer->update( 'constant', 'DB_NAME', $dbname, array( 'raw' => true) );
But, I get an error. I do believe that the WPConfigTransformer.php is loaded but when I try to instantiate, that is where I get the error. The ABSPATH constant is defined. (I was also thinking that would be a good thing to add to your examples, but maybe my use-case for this is not correct.)