Using the Settings API, how should I add multiple values to an option?

I’m trying to add/ update additional values to an option created with the Settings API. I’m trying to do this with my validation callback function, but I’m not getting very far. Here is my code:

function tccl_settings_option_validate( $input ) {
    add_option( 'tccl_settings_option', $input );
}

This is causing a pretty big error. How should I be doing this?

What I would like to do is use the validation callback to add values to the option array without overwriting it.

Solutions Collecting From Web of "Using the Settings API, how should I add multiple values to an option?"

Get the option, modify only the values you need to modify in it, then return the results.

function tccl_settings_option_validate( $input ) {
    $options = get_option('tccl_settings_option');
    // modify $options using data from $input as needed
    return $options;
}

On your tccl_settings_option_validate function you need to:

  • get an array of all existing options.
  • update only the changed.
  • return that array.

    so something like:

    function tccl_settings_option_validate( $input ) {
    
    //do regular validation stuff
    //...
    //...
    
    //get all options
    $options = get_option(THEMENAME . '_settings');
    //update only the neede options
    foreach ($input as $key => $value){
        $options[$key] = $value;
    }
    //return all options
    return $options;
    }
    

you don’t have to save the option, that’s done for you. your validation function should check input then return something.

function tccl_settings_option_validate( $input ) {
    // do some checking/formatting/whatever of $input
    // and then
    return $input;
}