Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Converting plugin options to one serialized setting in DB WordPress

  • SOLVED

I am trying to update a plugin that used many options in the wp_options table, on the next update i want it to serialize the options in a single row with the settings api but the code appears to be bugged and i can't find the problem. When updating, the old options don't convert and update.

Here's the code:


// default settings array
function mfbfw_defaults() {
$defaults_array = array(
'padding' => '10',
'overlayShow' => 'on',
'overlayColor' => '#666666'
);
return $defaults_array;
}

// register_activation_hook
function mfbfw_install() {

// If old options present: convert, and delete them
if ( get_option( 'mfbfw_padding' ) !== false ) {

// get old options from database and add new option in one array
$old_settings_array = array (
'padding' => get_option('mfbfw_padding'),
'overlayShow' => get_option('mfbfw_overlayShow'),
'overlayColor' => get_option('mfbfw_overlayColor'),
// New Settings
'titlePosition' => 'inside',
'titleColor' => '#333333',
'showNavArrows' => 'on'
);

// save old settings to database in a single serialized option
add_option( 'mfbfw', $old_settings_array );

// Get deprecated settings and delete them from database
$deprecated_array = array (
'mfbfw_padding',
'mfbfw_overlayShow',
'mfbfw_overlayColor'
);

foreach ( $deprecated_array as $key ) {
delete_option( $key );
}

// If no deprecated options present, then get default values
} else {

// Get default settings
$defaults_array = mfbfw_defaults();

// If no options stored, write defaults to database
add_option( 'mfbfw', $defaults_array );

}

// Update Version
update_option( 'mfbfw_active_version', FBFW_VERSION );

}
register_activation_hook( __FILE__, 'mfbfw_install' );


Any suggestions?

Answers (2)

2011-12-15

John Cotton answers:

SHould this

get_option( 'mfbfw_padding' ) !== false

be this:

get_option( 'mfbfw_padding' ) != false

?


John Cotton comments:

...probably more clearly written as:

if ( get_option( 'mfbfw_padding' ) ) {


José Pardilla comments:

I will try with that, although the code seems to work fine in my testing blogs but didn't work for many people which has forced me to remove the plugin update until i find the problem.


John Cotton comments:

Was it possible with the old plugin that that the mfbfw_padding option was never created? Just asking....:)


José Pardilla comments:

That's part of the reason i chose to use the !== false.

The options is always there but if a user empties the field and i just check "if ( get_option( 'mfbfw_padding' ) )" i might get a false negative.

What i don't understand is, if it gives false or positive, either way i will populate the new option (with old or default values), but the plugin shouldn't break, but it does break for some people and they had to use a 'reset settings' button to fix it.

I'll make a few test later this afternoon and see if i find anything new.


José Pardilla comments:

Finally figured it out.

Since WP3.1, register_activation_hook() isn't called when the plugin is updated automatically from the admin panel, it's only called when the plugin is manually activated.

More info here: http://wpdevel.wordpress.com/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/

Thanks for the help though John, i appreciate it. :)

2011-12-15

Luis Abarca answers:


// save old settings to database in a single serialized option
add_option( 'mfbfw', <strong>serialize</strong>($old_settings_array) );


José Pardilla comments:

It is my understanding that wordpress already serializes the array when using add_option()

The data is correctly saved nevertheless, the problem with the code is taht in some cases doesn't seem to make the conversion.