[wp-hackers] Best practices for upgrading plugin options?

Otto otto at ottodestruct.com
Thu Apr 28 20:35:41 UTC 2011


Steve, seems like you're understanding this wrong to begin with. You
don't need to add new options to the array and store them in some kind
of upgrade path. You need to change the point at where you get the
options to begin with to consider the default settings, for all cases
across the board. wp_parse_args will let you do that.

Consider a default set of settings:

$some_defaults = array(
'foo'=>'bar',
'alice'=>'bob'
);

Now, instead of storing the defaults in the database, you change where
you use the get_option to begin with to be like this:

$options = wp_parse_args(get_option('my_options'), $some_defaults);

The resulting $options will be a merge of the two, with anything not
in the my_options array being replaced with the stuff from
$some_defaults. You can store this result later using an update_option
if you want.

But you do this everywhere you get the option from the DB. This way,
you're doing the defaults on the retrieval and don't need to ever
handle "upgrading" at all. The defaults will be used if they're not
set in the db options row.

-Otto



On Thu, Apr 28, 2011 at 1:44 PM, Steve Bruner [SlipFire]
<sbruner at slipfire.com> wrote:
> Hi Sean,
>
> Thanks for the suggestion. The issue is that we are going to release future
> versions with different settings and defaults, and we don't know which
> version the user is upgrading from, so we would have to keep each default
> settings array in all future versions.  I was hoping for a cleaner solution.
>
> Steve
> _______________________________________________
> 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