有人能告诉我这个插件的设置页面哪里出了问题吗?

时间:2015-10-04 作者:dajoto

我一直在努力学习如何在业余时间制作插件。我之前已经完成了与设置页面相关的所有编码,看起来一切都很好。完成了测试插件的其他部分后,我返回了(几周后),现在当我保存对设置的调整时,我会看到一长串数据库字段名,其中返回了相关的输入/文本区域值,但没有保存调整。我相信没有其他的东西在使用add_action(\'admin_menu/admin_init\')

插件构建为一个类。以下是与设置页面相关的所有代码

class rifh_tester{

 public function __construct(){
  /* admin */
  if(is_admin()){
   add_action(\'admin_menu\', array($this, \'settings_add_menu\'));
   add_action(\'admin_init\', array($this, \'settings_admin_init\'));
  }
 }

 public function settings_add_menu(){
  add_options_page(\'RifH Tester Settings\', \'RifH Tester\', \'manage_options\', \'rifh_tester\', array($this, \'settings_opts_page\'));
 }

 public function settings_opts_page(){
  echo \'<div id="rifh-tester-setup"><form action="options.php" method="post">
       \' . settings_fields("rifh_tester_options") . do_settings_sections("rifh_tester") . \'
       <input name="submit" type="submit" value="Save Changes">
       </form>
       </div>\';
 }

 public function settings_admin_init(){
  register_setting(\'rifh_tester_options\', \'rifh_tester_options\', array($this, \'rifh_tester_options_validate\'));
  add_settings_section(\'rifh_tester_options_main\', __(\'Welcome to the RifH Tester Setup Page\'), array($this, \'rifh_tester_options_text\'), \'rifh_tester\');
  add_settings_field(\'rifh_tester_option_image_generate\', __(\'Test Creating An Image?\'), array($this, \'rifh_tester_option_image_generate\'), \'rifh_tester\', \'rifh_tester_options_main\');
  add_settings_field(\'rifh_tester_option_image_limit\', __(\'Test Define An Image Size\'), array($this, \'rifh_tester_option_image_limit\'),  \'rifh_tester\', \'rifh_tester_options_main\');
  add_settings_field(\'rifh_tester_option_appears_on\', __(\'Where Should Test Images Show?\'), array($this, \'rifh_tester_option_appears_on\'),  \'rifh_tester\', \'rifh_tester_options_main\');
  add_settings_field(\'rifh_tester_option_image_type\', __(\'Preferred Format For Test Images?\'), array($this, \'rifh_tester_option_image_type\'), \'rifh_tester\', \'rifh_tester_options_main\');
 }

 public function rifh_tester_option_image_generate(){
  echo "<label><input id=\'rifh_tester_option_image_generate\' name=\'rifh_tester_options[image_gen]\' type=\'checkbox\' ".($this->options[\'image_gen\'] ? \'checked>Yes\' : \'>No\')."</label>";
 }

 public function rifh_tester_option_image_limit(){
  echo "<label><input id=\'rifh_tester_option_image_limit\' name=\'rifh_tester_options[image_lim]\' type=\'text\' value=\'{$this->options[\'image_lim\']}\'> Width of each Linky box in pixels.</label>";
 }

 public function rifh_tester_option_appears_on(){
  echo "<label><input id=\'rifh_tester_option_appears_on\' name=\'rifh_tester_options[single]\' type=\'checkbox\' ".($this->options[\'single\'] ? "checked>Yes" : ">No")."</label>";
 }

 public function rifh_tester_option_image_type(){
  $html = "<select id=\'rifh_tester_option_image_type\' name=\'rifh_tester_options[image_type]\'>";
  $values = array(\'png\',\'jpg\',\'gif\');
  foreach($values as $format){
   $html .= "<option value=\'$format\'";
   if($format == $this->options[\'image_type\']){
    $html .= " selected";
   }
   $html .= ">$format</option>";
  }
  $html .= "</select>";
  echo $html;
 }

 public function rifh_tester_options_validate($input){
  $newinput = array();
  $newinput[\'image_gen\'] = false;
  if(isset($input[\'image_gen\'])){
   $newinput[\'image_gen\'] = true;
  }

  if(isset($input[\'image_lim\']) && preg_match(\'#^([1-9]([0-9]{1,2})?|1000)$#\', $input[\'image_lim\'])){
   // I\'ve excluded private isFloat function as it\'s a basic function of the class
   if($this->isfloat($input[\'image_lim\'])){
    $input[\'image_lim\'] = round($input[\'image_lim\'], 4);
   }
   $newinput[\'image_lim\'] = $input[\'image_lim\'];
  }

  if(isset($input[\'image_type\']) && preg_match(\'#^(jpg|png|gif)$#\', $input[\'image_type\'], $match)){
   $newinput[\'image_type\'] = $match[1];
  }

  $newinput[\'single\'] = false;
  if(isset($input[\'single\'])){
   $newinput[\'single\'] = true;
  }
  return $newinput;
 }

 public function rifh_tester_options_text($args){
  echo "<div id=\'{$args[\'id\']}\'></div>";
 }
}
当我实际查看设置页面上生成的HTML时,似乎<form> 显示在所有字段之后,只有提交输入。让我觉得错误在于settings_opts_page 类中定义的函数,但我看不出它是什么。

我已经重新阅读了所有关于WP设置API的指导,我不明白我做错了什么。有人能给我指出正确的方向吗?

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

所以今天再看一遍,我突然想到这肯定是settings_opts_page. 面对新的一天,答案显而易见settings_fieldsdo_settings_sections 旨在回显其内容,这会破坏页面呈现,并看到表单和内容被替换。

public function settings_opts_page(){
 echo \'<div id="rifh-tester-setup"><form action="options.php" method="post">
  \' . settings_fields("rifh_tester_options") . do_settings_sections("rifh_tester") . \'
  <input name="submit" type="submit" value="Save Changes">
  </form>
  </div>\';
}
应该是

public function settings_opts_page(){
 echo \'<div id="rifh-tester-setup"><form action="options.php" method="post">\';
 settings_fields("rifh_tester_options");
 do_settings_sections("rifh_tester");
 echo \'<input name="submit" type="submit" value="Save Changes"></form></div>\';
}

相关推荐