How can I add an options page for my class based plugin?

I’m trying to use the new Settings API with a class based plugin to add a settings page. This is the code I have so far:

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();

I’ve tried to keep that as clean and simple as possible, and I’ve tried to do it according to a lot of tutorials and documentation.

Read More

I also have a jQuery line in there to show hidden fields.

The only problem though is that this does not work. That is to say it adds a sample options page, but the field values are not there.

Screenshot:

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

Help! What am I doing wrong? I’ve been trying this for 24 hours now.

Related posts

Leave a Reply

1 comment