1

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.

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.

5
  • What exactly does "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." mean?
    – kaiser
    Jan 27, 2012 at 12:16
  • In the code I am trying to add a field called "Color Scheme". The sample page shows up in the menu, but there is no field for "Color Scheme" - just the default nonce options. Does that help?
    – cwd
    Jan 27, 2012 at 16:12
  • Yea, this is too localized in my opinion. Anyway: Do a return print $html instead of the echo.
    – kaiser
    Jan 27, 2012 at 17:40
  • @kaiser - you're probably right - might be good to return that instead of echoing it, but it still is not working. also i tried adding die() to the top of the color_scheme() function but the page still renders fine - footer and all - so for some reason that function is not even getting called
    – cwd
    Jan 30, 2012 at 6:06
  • @kaiser - got it working with the answer from @ungestaltbar, but wanted to confirm that i do in fact need to echo and not return as you suggested.
    – cwd
    Jan 30, 2012 at 9:00

1 Answer 1

2
+50

try to change

 do_settings_sections('sample_section1');

to

 do_settings_sections('sample');
2
  • perfect. unfortunately i can't award the bounty for another 24 hours...
    – cwd
    Jan 30, 2012 at 8:57
  • 2
    Don't worry :) When I got started with the settings api I felt like the codex terminology is a bit confusing: Think of it like this: On top you add a new settings page (add_menu_page), for this page you add sections (connected to the page id, and they are actually just groups sharing a headline, for example), for this sections you add fields (the actual input stuff) and finally you have to register your settings to tell wordpress that they exist and need to be handled. Hope this is explained correctly. Language knowledge is limited when it comes to technical stuff :) Jan 30, 2012 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.