I’m using the WordPress Settings API to create an options page for a plugin that handles images and links. I can register 3 add_settings_field calls and add 3 images and links to the site. Does anyone know of a way to dynamically let the user add a new field (add_settings_field) on the fly. For instance if it started with 1 field, but they needed a second for another picture.
Leave a Reply
You must be logged in to post a comment.
you could add a button and have your admin page handler (the last param to add_options_page for example) handle GET parameters so it makes a call to add_settings_field, maybe?
For the following to work you need to have one add_settings_field for the data. The html form elements within the callback function of the add_settings_field need to be an array.
Example:
If you design a form like this WordPress will store the data serialised in one option field, resulting less database request and you can add more data into into it later.
Then you use e.g. jQuery or JavaScript and a button to add more fields to the option page.
The additional fields will also be an array and numbering starts where the previous form fields ended. Looking at the previous code snipper you would start:
If you put stuff together fine your basic form with x amount of form fields is created in the callback function of add_settings_field. Additional fields are added with jQuery or Ajax.
Now you might have a case where you want to edit fields, in this case the form should show all previous stored values and have the text fields filled out.
You can do this by sending an attribute with the stored values to the add_settings_field. Have a look how I solved it for my self at WordPress settings API and serialising data