1

Issue is that the validation callback function for my plugin has NULL as the $input value.

It's my understanding that the validation callback will use the $input parameter, which contains the POST fields. But when I test the validation callback, the $input parameter is NULL, even though settings are checked.

Note that the settings page has just checkboxes; no other input field types.

My register_settings is as follows; it is tied to the admin_init hook.

register_setting( 
   'pluginPage',    // option group name
    'Settings Name',    // option name (used to store into wp-options
    array('sanitize_callback' => 'sanitize_callback' // sanitize the data function')
    );

And the sanitize_data callback:

 function sanitize_callback($input)
    { $new_input = array();
  // ... some checking here, setting $new_input array values if $input array value exists
  return $new_input;}

Because $input is NULL, which I have verified with a print_r($input);exit; at the beginning of the sanitize_callback() function, the options are not saved in wp-options.

The only way I can get options saved is to use $_POST variables in the sanitize_callback function. That will properly store the options.

So, why is the sanitize_callback function not receiving the $_POST variables when the form is submitted?

Note that I have followed the instructions in the Settings API; I have verified all code is correct according to those instructions. The form is displayed with this code, so the nonce is in the form.

<form action='options.php' method='post'>
        <?php
        settings_fields( 'pluginPage' );    // initializes all of the settings fields
        do_settings_sections( 'pluginPage' );   // does the settings section
        submit_button();    // creats the submit button
        ?>
    </form>
0

I guess in the meantime you figured in out already. Though I want to share my experience since I faced the same problem and it might be useful for others.

In my case I just got the reference to the options wrong when creating the HTML code for the input-elements.

So since Wordpress suggests to have one option saving an array of values that are the actual settings of your plugin/theme/whatever, you'll problaby have something like this somewhere: (Note: I will use plugin_config where you you have Settings Name, Rick.)

add_option('plugin_config', array(
    'val1' => 1,
    'val2' => 2'
));

Then in addition to registering the settings somwhere you will have registered the corresponding fields to enter values for the section, like this:

add_settings_field('val1_field', 'Field Label:', 'creat_html_callback', 'settings_page_slug_name');

Now the intresting part; when defining the html code, you have to set the name of the field in a way that wordpress can associate the corresponding post value of the request to the correct array position of the option plugin_config (your Settings Name!). So here is the callback referenced in add_settings_field before

public function creat_html_callback() {
    $option = get_option('plugin_config');
    echo '<input type="text" id="val1Field" name="plugin_config[val1]" size="10" value="'.$option['val1'].'"/>';
}

The essentiell part for me was name="plugin_config[val1]". The name of the html-input-element must be the same as the registered (register_setting!) option/setting or the name of the option together with the array index of the value corresponding to the input field. Of course this seems clear and easy to see. Bit I think whith all those slug-names and references you can get easily confused especially when following a tutorial like this.

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.