如何为我的基于类的插件添加选项页面?

时间:2012-01-26 作者:cwd

我正在尝试使用新的设置API和基于类的插件来添加设置页面。这是我目前掌握的代码:

class simple_sample_plugin{
    function simple_sample_plugin()
    {
        add_action(\'init\', array(&$this, \'init\'));
    }
    function init()
    {
        add_action(\'admin_menu\', array(&$this, \'admin_menu\'));
        add_action(\'admin_init\', array(&$this, \'admin_init\'));
    }
    function admin_init()
    {
        register_setting(
            \'sample\'  // A settings group name. Must exist prior to the register_setting call.
            ,\'sample_options\' //  The name of an option to sanitize and save.
            ,array($this, \'set_options\') //  A callback function that sanitizes the option\'s value.
        );
        // Register our settings field group
        add_settings_section(
            \'sample_section1\', // String for use in the \'id\' attribute of tags.
            \'Section 1\', //  Title of the section.
            \'__return_false\', // Function that fills the section with the desired content. The function should echo its output.
            \'sample\' //  The type of settings page on which to show the section (general, reading, writing, media etc.)
        );
        // Register our individual settings fields
        add_settings_field(
            \'color_scheme\'  // String for use in the \'id\' attribute of tags.
            ,\'Color Scheme\' // Title of the field.
            ,array($this, \'color_scheme\') // Function that fills the field with the desired inputs as part of the larger form. Passed a single argument, the $args array. Name and id of the input should match the $id given to this function. The function should echo its output.
            ,\'sample\' //  The type of settings page on which to show the field (general, reading, writing, ...).
            ,\'sample_section1\' // The section of the settings page in which to show the box (default or a section you added with add_settings_section, look at the page in the source to see what the existing ones are.)
            ,array() // $args - Additional arguments that are passed to the $callback function. The \'label_for\' key can be used to give the field a label different from $title.
        );
    }
    function set_options()
    {

    }
  function color_scheme()
    {
      echo "<textarea type=\'text\' rows=\'2\' cols=\'80\' name=\'sample_options[color_scheme]\'>";
      echo time();
      echo "</textarea>";
  }
    function admin_menu()
    {
        add_options_page(
            __(\'sample\') //page title
            ,__(\'sample\') //menu title
            ,\'edit_posts\' //capability
            ,\'sample\' //menu-slug
            ,array(&$this, \'settings_page\') //function callback
        );

    }
    function settings_page()
    { ?>
    <div class="wrap">
    <h2>sample Options</h2>
    <form class="myform" method="post" action="options.php"> 
    <?php

        settings_fields(\'sample\');
        do_settings_sections(\'sample_section1\');


    ?>
    <script type="text/javascript">
        jQuery(\'.myform input:hidden\').prop(\'type\',\'text\');


    </script>
    <input type="submit" value="Save" />
    </form>
    </div>
    <?php
    }
}
global $simple_sample_plugin;
$simple_sample_plugin = new simple_sample_plugin();
我一直在努力保持它尽可能的干净和简单,我也根据大量的教程和文档进行了尝试。

我还有一个jQuery行显示隐藏字段。

但唯一的问题是,这不起作用。也就是说,它添加了一个示例选项页面,但字段值不存在。

屏幕截图:

screenshot-with-shadow.png http://img600.imageshack.us/img600/4526/screenshotwithshadow.png

帮助我做错了什么?我已经试了24小时了。

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

尝试更改

 do_settings_sections(\'sample_section1\');

 do_settings_sections(\'sample\');

结束

相关推荐

WP 3.3 Tooltips API?

有没有人教过如何在主题或插件中实现新的工具提示API?我渴望利用这一点来帮助我的主题和插件的第一次用户。从…起mashable\'e excellent intro to 3.3...新的工具提示对更有经验的用户来说可能有点烦人,但对大多数人来说,它可能非常有用。工具提示弹出式气泡现在显示出来,帮助用户浏览WordPress中的新功能。升级后您将立即看到其中一个,它指向新的管理栏。> Developers are able to use these popups to add their own f