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 have one option on a settings page that can have between 1 and 5 values. I have a basic understanding on how to set up single value settings using the Settings API, but how do I go about generating the markup etc. for the setting with a variable number of values?

share|improve this question
    
Please could the person who voted this question down try and be constructive by leaving a comment saying why. I see nothing wrong with it myself, but would appreciate any input on how to improve it. –  ProfK Nov 30 '11 at 4:39

1 Answer 1

up vote 2 down vote accepted

If you are using the Settings API correctly, you should be using the add_settings_field function. The 3rd argument in this function is for the callback function that will generate the HTML for the form field for the individual setting. The Codex states:

Function that fills the field with the desired inputs as part of the larger form. Name and id of the input should match the $id given to this function. The function should echo its output.

In this callback function, you just write your HTML, just like you would if WordPress was not involved. As such, you can setup radiobuttons, a select field, multi-select, etc.

As an example, let's say that you use add_settings_field as such:

add_settings_field(
    'my_setting_name',
    'My Setting Name',
    'my_setting_callback_function',
    'general',
    'my_settings_section_name'
);

In this snippet, the callback function named "my_setting_callback_function" will generate the HTML for the setting. You can then do something like:

function my_setting_callback_function() {
?>
    <select name="my_setting_name" id="my_setting_name">
        <option value="1">Enabled</option>
        <option value="0">Disabled</option>
    </select>
<?php
}

As you can see, this function generates the different possible values for this setting.

I highly encourage reading this part of the Codex: http://codex.wordpress.org/Settings_API. It does a nice job explaining the Settings API and the example does a good job illustrating how to use it. I find that the API is a bit convoluted; however, it does what it says it should do, so with a little discipline and careful checking, it should work quite well for you.

Good luck!

share|improve this answer

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.