4

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!

3
  • 1
    If I've understood correctly you essentially want an option whose value is an array, and you want to add to that array instead of replacing it? So if the value is array('a','b') and you recieve 'c' in your input you want the new value to be array('a','b','c')? Mar 8, 2012 at 15:34
  • Stephen Harris, exactly! :) $valid_input[$id][] = 'old option'; $valid_input[$id][] = 'newer option'; $valid_input[$id][] = 'latest option'; outputs exactly what I want, but no idea how to use it with $input! :) I believe it should work just fine as $valid_input[$id][] = $input[$id]; but it doesn't. Mar 8, 2012 at 15:36
  • Do you mind me asking why? Might help with a solution. If you do the above you can then never remove values, but only add more. Mar 8, 2012 at 15:39

1 Answer 1

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];
3
  • Sorry, but I made this typo here on stackexchange. By the way I believe this mistake will output Parse error: syntax error, unexpected ';' anyways. But thanks for the help! Mar 8, 2012 at 15:35
  • @Wordpressor See update
    – kaiser
    Mar 8, 2012 at 15:40
  • It still returns NULL as well as $valid_input[$id] = array(); $valid_input[$id] = array_push($valid_input[$id], $input[$id]);. Check the update above. Mar 8, 2012 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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