需要帮助理解/使用设置API进行编码

时间:2011-05-28 作者:Jiew Meng

所以我做了下面这样的事情,但我得到的只是(我期待一个Facebook文本框?)

我希望它不要太乱。我不熟悉设置API。。。

add_action(\'admin_init\', function() {
    register_setting(\'elem_opts\', \'elem_opts\', \'elem_validate_opts\');
  add_settings_section(\'elem_opts_form\', \'Elements Theme Options\', \'elem_opts_form_cb\', \'elems_opts\');
  add_settings_field(\'elem_opts_form\', \'Facebook\', \'elem_opts_social_fb_cb\', \'elems_opts\', \'elem_opts_form\');
});

function elem_opts_social_fb_cb() {
  $opts = get_option(\'elem_opts\');
  if (empty($opts) || !is_array($opts)) {
    $opts = array();
  }
  ?>
  <input type="text" name="elem_opts[fb]" value="<?php echo $opts[\'fb\'] ? $opts[\'fb\'] : \'\'; ?>" />
  <?php
}

add_action(\'admin_menu\', function() {
    add_theme_page(\'Elements Options\', \'Elements Options\', \'manage_options\', \'elem_opts\', \'elem_opts_cb\');
});

function elem_validate_opts($input) {
  return $input;
}

function elem_opts_form() {
  ?>
  <p>Enable social network links, by entering your username for the respective social networks</p>
  <?php
}

function elem_opts_cb() {
    ?>
    <div class="wrap">
        <h2>Elements Theme Options</h2>

        <form action="options.php" method="post">
            <?php 
            settings_fields(\'elem_opts\'); 
            do_settings_sections(\'elem_opts\');
            ?>
            <input type="submit" name="Submit" value="Save changes" />
        </form>
    </div>
    <?php
}

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

您有一些回调函数,但ID混淆了,请尝试以下操作:

add_action(\'admin_init\', function() {
    register_setting(\'elem_opts\', \'elem_opts\', \'elem_validate_opts\');
    add_settings_section(\'elem_opts_form1\', \'Elements Theme Options\', \'elem_opts_form\', \'elem_opts\');
    add_settings_field(\'elem_opts_form\', \'Facebook\', \'elem_opts_social_fb_cb\', \'elem_opts\', \'elem_opts_form1\');
});

function elem_opts_social_fb_cb() {
  $opts = get_option(\'elem_opts\');
  if (empty($opts) || !is_array($opts)) {
    $opts = array();
  }
  ?>
  <input type="text" name="elem_opts[fb]" value="<?php echo $opts[\'fb\'] ? $opts[\'fb\'] : \'\'; ?>" />
  <?php
}

add_action(\'admin_menu\', function() {
    add_theme_page(\'Elements Options\', \'Elements Options\', \'manage_options\', \'elem_opts\', \'elem_opts_cb\');
});

function elem_validate_opts($input) {
  return $input;
}

function elem_opts_form() {
  ?>
  <p>Enable social network links, by entering your username for the respective social networks</p>
  <?php
}

function elem_opts_cb() {
    ?>
    <div class="wrap">
        <h2>Elements Theme Options</h2>

        <form action="options.php" method="post">
            <?php 
            settings_fields(\'elem_opts\'); 
            do_settings_sections(\'elem_opts\');
            ?>
            <input type="submit" name="Submit" value="Save changes" />
        </form>
    </div>
    <?php
}
这给了我:

enter image description here

下次尝试为每个函数、回调、节和页段塞使用不同的名称和ID。

SO网友:Chip Bennett

参考请参见此处:Incorporating the Settings API in WordPress Themes. 具体见第10页。

您的第一个问题,虽然我不知道它会导致表单节/字段不输出,但您将与第一个参数相同的字符串传递给这两个add_settings_section()add_settings_field(). 对于add_settings_field(), 第一个参数是设置字段本身的ID,因此它可能与传递给的第一个参数不同add_settings_section(), 它是设置部分的ID。

结束

相关推荐

Wp_Options中的临时RSS提要没有自动删除吗?

我刚刚注意到55000个条目(!)在我的wp\\U选项表中。我有一阵子没去那里了。所以我跑了:delete from `wp_options` where `option_name` like \'_transient_timeout_rss%\' delete from `wp_options` where `option_name` like \'_transient_rss_%\' 还有。。。现在返回到645个条目。。。既然transient似乎是永恒的,我怎么能自动删除这些旧的R