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.

I was building a theme options page with the settings API. I use an ajax call to compute some settings automatically from other pages / tabs. I noticed that all of the new settings default ("std") will display in their input fields but will be undefined when I make the ajax call using :

array_merge(get_option('some_options', 'fail'), get_option('some_other_options', 'fail'));

That is they will be undefined until the first time I save the new settings. All of the previously defined settings will be rendered in the resulting object, but all of the newly authored additions will be undefined.

I was wondering if the option's defaults are saved when you switch to the theme for the first time.

So when I add new options while using that theme the options defaults are not being saved. I am concerned, if that is the case, that some key values would be omitted when a user first activates this theme.

So just to be clear on what is happening.

1) I have some options to start with. -> everything is good.

2) I author some new options. -> trouble starts

3) Settings page loads. Default values are in the input fields but, when I make the call and check the database. "undefined" is returned for those new settings. All the other settings are fine.

4) Reload page. -> no change

5) Save changes.

6) Ajax call is fine, all values defined.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Don't rely on the options ever getting saved. Better is for the Theme to function normally, whether or not the user ever visits the Theme Settings page.

First step is to define a function to return your option defaults, either:

  1. Define them in the function, and then reference that function in your option parameters array

    function mytheme_option_parameters() {
        $parameters = array(
            'foo' => array( 
                'name' => 'foo',
                'title' => 'Foo',
                'std' => 'bar'
            ),
            'foo1' => array( 
                'name' => 'foo1',
                'title' => 'Foo1',
                'std' => 'bar1'
            ),
         );
         return $parameters;
    )
    
    function mytheme_option_defaults() {
        $parameters = mytheme_option_parameters;
        $defaults = array();
        foreach ( $parameters as $param ) {
            $defaults[$param['name']] = $param['std'];
        }
        return $defaults;
    } 
    
  2. Define them in your option parameters array, and then loop through that array inside your function.

    function mytheme_option_defaults() {
        $parameters = mytheme_option_parameters;
        $defaults = array();
        foreach ( $parameters as $param ) {
            $defaults[$param['name']] = $param['std'];
        }
        return $defaults;
    } 
    
    function mytheme_option_defaults() {
        $defaults = array(
            'foo' => 'bar',
            'foo1' => 'bar1'
        );
        return $defaults;
    } 
    
    function mytheme_option_parameters() {
        $defaults = mytheme_option_defaults();
        $parameters = array(
            'foo' => array( 
                'name' => 'foo',
                'title' => 'Foo',
                'std' => $defaults['foo'];
            ),
            'foo1' => array( 
                'name' => 'foo1',
                'title' => 'Foo1',
                'std' => $defaults['foo1']
            ),
         );
         return $parameters;
    )
    

Then, when you need to reference your Theme options, do this:

function mytheme_get_options() {
    $defaults = mytheme_option_defaults();
    $options = wp_parse_args( get_option( 'theme_themename_options', array() ), $defaults );
    return $options;
}   

The magic here is the wp_parse_args() call. Basically, if no Theme options are set, then the defaults are used, but if the options are set, then they are used. Any options not set by the user (e.g. a newly added option) will fallback to use the Theme-defined default.

The Theme works out-of-the-box, using Theme-defined defaults, even if the user never visits the Theme Settings page. And if the user never actually saves any settings, the Theme never even writes a DB entry for its options.

share|improve this answer
    
Thank you so much. I thought I was just being paranoid. It seems like something with such a simple solution would be built into the theme activation or at least built into get_option(). Again, thank you. –  Chris Feb 23 '13 at 17:26

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.