No subject


Sun Feb 20 18:20:38 UTC 2011


You must have a serialized array in the _options db table that is pulled
from a single call to get_option(); Upon upgrade, you're wanting to add
array keys to this options array without overwriting anything that you're
users have changed.

The best way that I can see to do this is to use wp_parse_args(). This
function should be used when the users options are pulled from the db, and
before any option is used. Here's an example.

//this would be pulled from the db with $user_adjusted_settings =
get_option( 'user_adjusted_setting' );
$user_adjusted_settings = array(
  "setting1" => "on",
  "setting2" => "This is a text field"
);

//you can upgrade these all that you want
$default_settings = array(
  "setting1" => "on",
  "setting2" => "This is a text field",
  "setting3" => "style1"
);

$options_to_use = wp_parse_args( $user_adjusted_settings, $default_settings
);

This would allow you to forget the need to build an upgrade function.

Sincerely,

Jonathon Byrd | Partner | Senior Software Engineer
5 Twenty Studios, LLC

C: (360) 747-7401 | O: (503) 268-1177 | F: (954) 867-1177
jon at 5twentystudios.com | www.5TwentyStudios.com<http://www.5twentystudios.com/>



On Thu, Apr 28, 2011 at 11:36 AM, Sean Newham <seanseviltwin at gmail.com>wrote:

> It's probably not best practice, but why don't you keep your current
> setting/installation configuration, then append the extra details to the
> array when an upgrade is performed using array_merge?
> On 28 Apr 2011 19:32, "Steve Bruner [SlipFire]" <sbruner at slipfire.com>
> wrote:
> > Hi Everyone,
> >
> > Any help with this would be greatly appreciated.
> >
> > Our current plugin has over ten releases, some major and some minor. For
> > most of the versions the plugin options have remained the same. We keep
> the
> > default settings in an array. When the plugin is initially activated we
> add
> > the defaults using add_option. Here's an example of the array.
> > $default_setings = array(
> >
> > "setting1" => "on",
> >
> > "setting2" => "This is a text field"
> >
> > )
> >
> > Now we are coming out with another version of the plugin, and we're
> adding
> > more settings, and creating the upgrade function. Writing the function to
> > trigger on the upgrade is not a problem. However, we're having an issue
> > figuring out the best way to upgrade the actual settings. The new
> > default_settings array would look like this:
> > $default_setings = array(
> >
> > "setting1" => "on",
> >
> > "setting2" => "This is a text field",
> > "setting3" => "style1"
> >
> > )
> >
> > First we thought we could just run the default_settings array on upgrade
> as
> > well using update_option. This would ignore setting1 and setting2 since
> they
> > already exist and just add setting3. However, if the user decides to
> > uncheck setting1 or delete the text in setting2, running update_option
> will
> > add them back in using the defaults.
> >
> > We're planning on adding lots of new options in future versions, so
> creating
> > different settings arrays for each version seems a bit tedious.
> >
> > If anyone has any best practices or examples on how to implement this, I
> > would appreciate it.
> >
> > Thanks
> > Steve
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list