Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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

share|improve this question
    
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 '12 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 '12 at 16:12
    
Yea, this is too localized in my opinion. Anyway: Do a return print $html instead of the echo. –  kaiser Jan 27 '12 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 '12 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 '12 at 9:00

1 Answer 1

up vote 2 down vote accepted
+50

try to change

 do_settings_sections('sample_section1');

to

 do_settings_sections('sample');
share|improve this answer
    
perfect. unfortunately i can't award the bounty for another 24 hours... –  cwd Jan 30 '12 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 :) –  ungestaltbar Jan 30 '12 at 9:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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