I don’t have a plugin to point you to off the top of my head, but my general suggestion is this: Store the relevant plugin settings in a single option, as an array. The crux of the reasoning here is that you’re just adding items to a single array, which is auto-keyed, so you can add or subtract at will, without worrying about naming.
In practice, this means the following:
-
Build your markup so that the textboxes have the
name
attribute of a shared array, eg<input name="my_textboxes[]" /> <input name="my_textboxes[]" />
-
When saving with AJAX, make sure you send the whole
my_textboxes
array as payload -
Your callback for saving the settings can then be straightforward. Since the entire my_textboxes array gets sent on every save, you can just save the whole shebang to the options table, overwriting previous settings:
$my_textboxes = $_POST['my_textboxes']; update_option( 'my_plugin_textboxes', $my_textboxes );
Of course, you may want to loop over the value passed by
$_POST['my_textboxes']
and do some sort of validation (check for duplicates, stuff like that). -
When you’re building the markup on page load, you’ll just do a foreach loop:
$my_textboxes = get_option( 'my_plugin_textboxes' ); foreach( $my_textboxes as $key => $tb ) { echo '<input type="text" name="my_textboxes[]" value="' . esc_attr( $tb ) . '" />'; }