1

I am struggling to get my settings page to save my options. I've got it showing up correctly and it hits the sanitize function

    register_setting(
        'my_options', // Option group
        'my_settings', // Option name
        array($this, 'sanitize') // Sanitize
    );

When I run the debugger on the sanitize function, $input is null:

public function sanitize($input)
{
    $new_input = array();

    $new_input = $input;
    //Sanitize the input actually

    return $new_input;
}

The form itself is called like this:

<form id="my-admin-form" method="post" action="options.php">
    <!-- Submission Notices -->
    <div class="status-box notice notice-info" style="display: none;"></div>
    <?php
    settings_fields('my_options');
    do_settings_sections('my_section');
    submit_button();
    ?>
</form>
  • Were you able to find the answer? – Vaviloff Oct 18 '16 at 13:11
  • I think I did, but I'm not sure any more and forgot which project this was in, so can't look it up. – dhuyvetter Oct 19 '16 at 10:24
  • 1
    It might have been a nonce issue, where I was checking against a nonce that wasn't created. codex.wordpress.org/WordPress_Nonces – dhuyvetter Oct 19 '16 at 10:27
1

It turned out to be nonce related: the form didn't have a nonce. Solved this by adding wp_nonce_field to the form.

1

I had a similar problem even after I added the nonce.

The mistake I'd made was that the input names need to be an array, for example:

<input id="my_option[key]" type="text" name="my_option[key]" value="<?php !empty(my_option[key]) ? my_option[key] : NULL; ?>">

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.