1. Basic settings API callback.

Using Settings API my $sanitize_callback validating function looks like:

    (...)
    if($type == "foo") {
       $valid_input[$id] = $option[$id];
    }  

    else if($type == "bar") {
       $valid_input[$id] = wp_filter_nohtml_kses($input[$id]);
    } 
    (...)

It’s almost the same as in this great theme by Chip Bennett: https://github.com/chipbennett/oenology/blob/master/functions/options-register.php#L95

2. Avoiding option updating.

After form submit all options are being updated and it’s values are being overwritten with $input.

What if I don’t want to update one of them (let’s say it’s $type is multiple_settings) but instead create an array and add new options to it? How do I do that?

I was trying things like:

    else if($type == "multiple_settings") {
       $valid_input[$id][] = $input[$id]; // creates an array but still overwrites
       $valid_input[$id] = array_push($valid_input[$id], $input[$id]); //returns NULL
     } 

With no luck.

[edit]

Maybe it has something to do with the way I save my settings (return of validation function below)?

    $options = get_option('XX_theme_settings'); 
    $valid = array_merge($options,$valid_input);
    return $valid; 

Thanks!

1 Answer
1

Always develop with WP_DEBUG set to TRUE. You’ve a typo:

array_push($valid_input[$id], $input[$id];

(missing ) after the array_push)


You can’t push to arrays that are not … arrays.

// So, check this before pushing: 
$valid_input[$id] = ! is_array( $valid_input[$id] ) ? (array) $valid_input[$id] : $valid_input[$id];

Leave a Reply

Your email address will not be published. Required fields are marked *