0

I am working on a plugin update and have changed the settings option_name and key.

I would like that the old options are transferred to the new option_name.

The old option name is: plugin_options

The new being: plugin_settings_general

The keys have changed too, from "ga_accountId" to "ga_account_id".

I looked at $wpdb and update_option but could not working any thing out that worked.

This should work if I did not need to change the keys too.

// Get entire array
$plugin_options = get_option( 'plugin_options' );
// Update entire array
update_option( 'plugin_name_settings_general', $plugin_options );
// Delete old array
delete_option( 'plugin_options' );

1 Answer 1

1

I think changing array keys is more of a PHP problem

// Get entire array
$plugin_options = get_option( 'plugin_options' );

$new_options = array();

if( isset( $plugin_options['ga_accountId'] ) )
    $new_options['ga_account_id'] = $plugin_options['ga_accountId'];

// Update entire array
update_option( 'plugin_settings_general', $new_options );
// Delte old array
delete_option( 'plugin_options' );

Any end-user who might be using using your plugin's options for something in their theme, will get errors. But if you are going to rename things, plugin_settings_general should be name-spaced with your plugin's actual name.

4
  • Thanks, it looks good. I will test it soon. I did not want the plugin name here. I did update my question to make it clearer I hope.
    – grappler
    Jul 14, 2013 at 12:26
  • 1
    I would comment out the delete_option while you are testing... otherwise you could delete the data before you are sure it works! Jul 14, 2013 at 12:27
  • Great! I wasn't sure if you had other keys in the array. If so, just repeat the process. If not, why have it in an array at all? Jul 14, 2013 at 13:39
  • Yes, I did but wanted to keep the question simple.
    – grappler
    Jul 14, 2013 at 13:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.