I'm working on a plugin and having some issues validating input on its settings page. I'll try to explain as briefly as possible what I'm doing in the plugin file:

if (is_admin()) {
    add_action('admin_init', 'register_mysettings');
    add_action('admin_menu', 'my_menu');

function t1k_twitter_menu() {
    add_options_page('My Plugin Options', 'My Plugin', 'manage_options', 'my_plugin', 'my_plugin_options');

function t1k_twitter_options() {
    // include my form
    include 'my_plugin_options.php';

function register_mysettings() { // whitelist options
    register_setting('my-group', 'setting_1', array($this, 'validate'));
    register_setting('my-group', 'setting_2', array($this, 'validate'));

function validate($input) {
    // This is where I want to do some validation...

From various examples I've seen, it seems that I should be receiving a key-value array as $input in the validate function. What I actually see is a simple string containing the posted value.

Have I misunderstood, or is my implementation wrong? The options will save ok (if I remove my validate function, or just set it to return the posted value).


  • 3
    Have you seen this answer? Also, there is a typo in your code.
    – fuxia
    May 24, 2013 at 14:43
  • 1
    Please post your actual validation function code, rather than // This is where I want to do some validation...; otherwise, it will be impossible to answer your question. (Also, fix the syntax error on the add_options_page() line.) May 24, 2013 at 14:46
  • toscho, I had not seen that answer, thanks - that could help, I will give it a try. For reference, is that documented somewhere in the WordPress docs? May 24, 2013 at 18:47
  • Chip, there is no validation code - the value of $input is only ever the string entered in the form field (with no way of identifying the field) so I haven't written any yet. May 24, 2013 at 18:50
  • Well, if you define a validation callback, and pass it $input, but don't return $input, then the Settings API won't ever be able to save your settings to the database. May 24, 2013 at 19:17


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.