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.

How to add default values for theme options? Currently I use basic option from this site

The sample will stored option value in serialize on database. When we activate the theme, there is no value in database at first time until we click Save Changes button.

Now I want add default value when theme activate something like this.

option_id   blog_id     option_name     option_value                                              autoload 
152           0           theme_data      a:16:{s:12:"html_doctype";s:1:"1";s:14:"header_fav...     yes

Or how do I tweak current sample option to save not in serialize to database

option_id   blog_id     option_name     option_value    autoload 
152           0           html_doctype    1               yes

Let me know.

share|improve this question
    
How are your options generated, are they stored in one big array? Settings defaults is a matter of checking if data exists in the option, and setting it when it doesn't.. –  t31os Feb 4 '11 at 0:07
    
@t31os yes stored in one big array. register_setting() –  wow Feb 4 '11 at 2:42

1 Answer 1

up vote 1 down vote accepted

Loop over your array of options and generate a set of defaults from that..

Let's imagine your array looks something like this.. (but obviously bigger)..

$your_options = array(  
    array( 
        'name' => 'option-one',
        'desc' => 'Option one description',
        'type' => 'text',
        'std'  => 'default_value'
    ),
    ..and so on.. for each option..
);

First check if you have options, if not, it's the first install so you can set defaults.

$my_var_that_holds_options = get_option( 'your_theme_options_name' );
if( !$my_var_that_holds_options ) {
    // Do code to setup defaults
}

The code to setup defaults will of course go where i've put // Do code to setup defaults.

First, create an array to hold the defaults and loop over the options array to build the new defaults array.

$defaults = array();
foreach( $your_options_var as $option_data ) {
    $defaults[$option_data['name']] = $option_data['std'];
}

By the end you'll have an array of option names and default values, which you can then send to the database.

update_option( 'your_theme_options_name', $defaults );

If you code needs to loop over the options to set boxes checked, etc.. simply call get_option again after you've saved the defaults..

$my_var_that_holds_options = get_option( 'your_theme_options_name' );

Or use the defaults array.

$my_var_that_holds_options = $defaults;

I've had to be quite general with the example, because the approach will differ depending on how your options array is structured.

The important factor here is to ensure your array of options holds a "default" value(std in the example) for each option else you've got nothing to form your default values from.

I've converted themes to do this a handful of times, if you have trouble implementing this feel free to pastebin your file with the options array and i'll take a look.

share|improve this answer
    
Thank you very much for the explanation. I really appreciate it. Make me clear in some part. Let me try to implement this approach. Need a time for me to understand everything :) just beginning. BTW this is pastebin.com/K4Hz6rDA a sample theme option I'm working on it. I want stored 5 defaults value after theme activate. –  wow Feb 4 '11 at 13:21
    
It might be easier for you to build off a more complete code base(theme shaper code is only a sample/example) that has the bulk of the work done, the devinsays / Options-Framework linked to in: wordpress.stackexchange.com/questions/7472/… might be a good starting point for you. –  t31os Feb 4 '11 at 13:40
    
yes I was tried devinsays option pages. make me dizzy to know from zero to hero :) –  wow Feb 4 '11 at 13:55
    
looks like I need to follow another approach like your answer. thanks t31os –  wow Feb 4 '11 at 14:39

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.