Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What is the best way to go about setting up default values for a plugin? Should I just insert those values into the wp_options table?

AND...if that's the best way to go about it, I have another question. My options are listed as a group which currently looks like:

a:4:{s:26:"nc_location_drop_down_zoom";s:2:"14";s:17:"nc_location_width";s:3:"200";s:29:"nc_location_drop_down_maptype";s:7:"roadmap";s:11:"text_string";s:0:"";}

Is this a serialized array? How do I do an insert like this into the table? (I realized this is more of an sql question...)

share|improve this question
    
FYI: WordPress functions deal with serialization for you, depending on where you've stored that data will depend what function you need, but assuming an option, calling get_option will be sufficient in unserializing the data. –  t31os Aug 16 '11 at 23:09
    
you can do like they say here wordpress.org/support/topic/… –  user10879 Dec 6 '11 at 0:48
add comment

2 Answers

up vote 0 down vote accepted

Use the Settings API and save your data in a single option as an array, WordPress will serialize the data for you.

share|improve this answer
    
I'm using the Settings API to set up my options...but when I try to set the default in my callbacks it just overrides the user choices...is there a specific method in the API that is used to set the defautls. –  redconservatory Aug 16 '11 at 23:22
1  
you just need to check if the option already exists, and only write the defaults if it doesn't. this is typically done in a register activation hook. –  Milo Aug 16 '11 at 23:32
add comment

You should do defaults at the time of pulling the data out. Never insert default values into the database. Defaults are default. Options in the DB override defaults.

How to do defaults for a serialized options array:

$defaults = array(
  'default1' => '1',
  'default2' => '2',
);
$options = wp_parse_args(get_option('plugin_options'), $defaults);
share|improve this answer
1  
And when the array is more complex, will wp_parse_args deal with the data correctly? ie. in cases where you have arrays of arrays and so forth. –  t31os Aug 17 '11 at 9:00
    
It will only be looking at the top level of the array and whether the keys exist or not. If you're nesting arrays for options, you may be making it overly complicated. –  Otto Aug 17 '11 at 16:00
1  
I don't know about being overly complicated, it's quite reasonable that i may choose to store other data with those option names and values, as is done with many theme options, for example you may store info about the type of option(checkbox, text, etc), a description and perhaps more. I guess you could always create a secondary relational array to hold that data though(but i think that itself might be overly complicating the task). –  t31os Aug 17 '11 at 16:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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