0

I'm making a plugin with an administration page witch uses the Settings API. I could make one section and one option. But how can I add other form fields to it?

If I add an input tag to the callback function, it adds it to the same field. Does that mean that if I want to add another field, do I have to create another function? I think that's too much for saving one option value. So that must not be it.

So could you modify this sample plugin, which is very simple, to add another form option? Also I'd like to add another section as well.

<?php /* Plugin Name: Settings API Demo Description: Learning Setting Field and Settings Section Author: Teno */ add_action('admin_init', 'settingsapi_init'); function settingsapi_init(){ register_setting( 'settingsapi_optiongroupname', 'settingsapi_optionname'); add_settings_section('plugin_main', 'Section 1', 'settingsapi_sectiondescription', 'settingsapi_pageslug'); add_settings_field('plugin_text_string', 'Option A', 'settingsapi_setting_string', 'settingsapi_pageslug', 'plugin_main'); add_settings_field('plugin_text_string', 'Option B', 'settingsapi_setting_string', 'settingsapi_pageslug', 'plugin_main'); } function settingsapi_sectiondescription() { echo '<p>This is a section description.</p>'; } function settingsapi_setting_string() { $options = get_option('settingsapi_optionname'); echo "<input id='plugin_text_string' name='settingsapi_optionname[option_a]' size='40' type='text' value='{$options['option_a']}' />"; // echo "<input id='plugin_text_string' name='settingsapi_optionname[option_b]' size='40' type='text' value='{$options['option_b']}' />"; // <-- this is not what I want. } // admin menu add_action('admin_menu', 'plugin_admin_add_page'); function plugin_admin_add_page() { add_options_page('Custom Plugin Page', 'Demo Settings API Menu', 'manage_options', 'settingsapi_pageslug', 'settingsapi_adminpage'); } function settingsapi_adminpage() { ?> <div class="wrap"> <h2>Demo Plugin for Settings API</h2> <form action="options.php" method="post"> <?php settings_fields('settingsapi_optiongroupname'); ?> <?php do_settings_sections('settingsapi_pageslug'); ?> <?php submit_button(); ?> </form> </div> <?php } ?> 

    1 Answer 1

    0

    You're using same callback function for both setting fields. Use different ones with it's own inputs. Here's updated code:

    <?php /* Plugin Name: Settings API Demo Description: Learning Setting Field and Settings Section Author: Teno */ add_action('admin_init', 'settingsapi_init'); function settingsapi_init(){ register_setting( 'settingsapi_optiongroupname', 'settingsapi_optionname'); add_settings_section('plugin_main', 'Section 1', 'settingsapi_sectiondescription', 'settingsapi_pageslug'); add_settings_field('plugin_text_string_a', 'Option A', 'settingsapi_setting_string_a', 'settingsapi_pageslug', 'plugin_main'); add_settings_field('plugin_text_string_b', 'Option B', 'settingsapi_setting_string_b', 'settingsapi_pageslug', 'plugin_main'); } function settingsapi_sectiondescription() { echo '<p>This is a section description.</p>'; } // First field callback. function settingsapi_setting_string_a() { $options = get_option('settingsapi_optionname'); echo "<input id='plugin_text_string' name='settingsapi_optionname[option_a]' size='40' type='text' value='{$options['option_a']}' />"; } // Second field callback. function settingsapi_setting_string_b(){ $options = get_option('settingsapi_optionname'); echo "<input id='plugin_text_string' name='settingsapi_optionname[option_b]' size='40' type='text' value='{$options['option_b']}' />"; } // admin menu add_action('admin_menu', 'plugin_admin_add_page'); function plugin_admin_add_page() { add_options_page('Custom Plugin Page', 'Demo Settings API Menu', 'manage_options', 'settingsapi_pageslug', 'settingsapi_adminpage'); } function settingsapi_adminpage() { ?> <div class="wrap"> <h2>Demo Plugin for Settings API</h2> <form action="options.php" method="post"> <?php settings_fields('settingsapi_optiongroupname'); ?> <?php do_settings_sections('settingsapi_pageslug'); ?> <?php submit_button(); ?> </form> </div> <?php } ?> 
    7
    • So do I have to create a function per option? Isn't it too much? What if there are 20 options to save? Create 20 functions for it?
      – Teno
      CommentedAug 20, 2012 at 7:57
    • No that's not required, but if you want each option be a new settings field, then yes. You can also group some similar options into on field, I usually do it with related checkboxs.
      – Mamaduka
      CommentedAug 20, 2012 at 9:03
    • Would you mind showing an example? If I put more than one form inputs in one field function-callback, then the layout gets largely indented. Also, each callback function has this line $options = get_option('settingsapi_optionname'); Loading an option for one option looks ridiculous when the number of options becomes large.
      – Teno
      CommentedAug 20, 2012 at 9:22
    • @Teno here's example from one of my plugins - gist.github.com/3403629. You should save options as array, so you won't have problems with options number.
      – Mamaduka
      CommentedAug 20, 2012 at 12:18
    • I see you are using check boxes. What if I want to put three more text fields with a label per each? The layout gets messed up. The form area is restricted to have a large left margin. It seems I have to use another function call for them.
      – Teno
      CommentedAug 20, 2012 at 13:14

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.