选项验证器输入是否为假?

时间:2013-02-16 作者:TheWebs

我有一张这样的表格:

<form action="options.php" method="post">
  <ul class="nav nav-tabs">
    <li>
      <a href="#content" data-toggle="tab">content</a>
    </li>
    <li>
      <a href="#contentversion2" data-toggle="tab">content version 2</a>
    </li>
  </ul>
  <div class="tab-content">
    <div class="tab-pane active" id="content">
      <div class="controls">
        <input type="url" name="aisis_core[index_more_posts]" placeholder="Url" />
      </div>
      <div class="form-actions">
        <input type="submit" value="Submit" />
      </div>
    </div>
    <div class="tab-pane" id="contentversion2">
      content for this tab as well
    </div>
  </div><input type="hidden" name="option_page" value="aisis_options" /><input type=
  "hidden" name="action" value="update" /><input type="hidden" id="_wpnonce" name=
  "_wpnonce" value="0360d57793" /><input type="hidden" name="_wp_http_referer" value=
  "/wordpress/wp-admin/admin.php?page=aisis-core-options&amp;settings-updated=true" />
</form>
问题是,当我点击submit时,会根据注册的设置调用以下选项验证器函数:

public function option_validator($input){
    var_dump($input); exit;
    $option = get_option(\'aitisis_core\');
    $option = $input;
    update_option(\'success_message\', true);
    return $option;
}
从var转储中可以看到的问题是,输入总是空的。。。。无论我在url输入中输入什么。

我将设置注册为:

public function settings(){
    register_setting(
        \'aisis_options\', 
        \'aisis_options\', 
        array(
            $this, 
            \'option_validator\'
        )
    );
}
有什么建议吗?

1 个回复
最合适的回答,由SO网友:birgire 整理而成

这种设置框架适用于我处理自定义选项页面输入:

<?php
/*
Plugin Name: Settings Skeleton
*/

if(!class_exists(\'MySettings\')){    

    function call_my_settings() {
        return new MySettings();
    }

    if (is_admin()){
        add_action( \'init\', \'call_my_settings\' );
    }

    class MySettings{

        public function __construct(){
            add_action(\'admin_menu\', array(&$this, \'add_menu\'));
            add_action(\'admin_init\', array(&$this,\'settings\') );
        }

        public function add_menu(){
            add_options_page(__(\'My Plugin Settings\',\'myplugindomain\'), \'My Plugin Settings\', \'manage_options\', __FILE__,  array(&$this,\'options_page\'));
        }   

        public function settings() {
            register_setting( \'my_settings\', \'my_settings\', array(&$this,\'validate\') );
        }

        public function validate($input) {
            // do validation
            $input[\'mytext\']=esc_attr($input[\'mytext\']);
            return $input;
        }

        public function options_page(){
        ?>
        <div class="wrap">
            <div class="icon32" id="icon-options-general"><br></div>
            <h2><?php _e(\'My plugin settings\',\'myplugindomain\'); ?></h2>
            <form method="post" action="options.php">
                <?php settings_fields(\'my_settings\'); ?>
                <?php $settings = get_option(\'my_settings\'); ?>
                <table class="form-table">
                    <tr>
                        <th scope="row" colspan="2">
                            <h3><?php _e(\'General settings\',\'myplugindomain\'); ?></h3>
                        </th>
                    </tr>
                    <tr>
                        <th scope="row"><?php _e(\'My text\',\'myplugindomain\'); ?> </th>
                            <td>
                                <input type="text" size="70" name="my_settings[mytext]" value="<?php echo $settings[mytext]; ?>" />
                             </td>
                     </tr>
                </table>
                <?php submit_button(); ?>
             </form>
         </div>
        <?php
        }
    }
}
这将为您提供以下选项页面:

settings menu

settings

你怎么把你的settings() 作用

在上面的代码示例中settings() 函数连接到admin_init 行动,即。

add_action(\'admin_init\', array(&$this,\'settings\') );

结束