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?
Leave a Reply
You must be logged in to post a comment.
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: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:In this snippet, the callback function named “my_setting_callback_function” will generate the HTML for the setting. You can then do something like:
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!