I am developing an application (plugin) where the user needs to enter any number of number of phrases in the settings page and these will be used by the plugin.
I have implemented this to capture the input values as follows:
register_setting('my_group', 'my_options', 'my_validate');
The settings page points to the input form as:
function my_options_page() {
echo '<div class="wrap">';
echo '<h2>'.esc_html( get_admin_page_title() ).'</h2>';
echo '<h3>Setup / Settings</h3>';
settings_errors();
echo '<form action="options.php" method="post">';
settings_fields("my_group");
echo '<table class="form-table">';
echo '<tr valign="top"><th colspan="2">Data Input</th></tr>';
echo '<tr valign="top"><th scope="row">Phrase [1]</th><td><input name="my_options['phrases'][0]" value="" size="32" type="text"></td></tr>';
echo '<tr valign="top"><th scope="row">Phrase [2]</th><td><input name="my_options['phrases'][1]" value="" size="32" type="text"></td></tr>';
echo '<tr valign="top"><th scope="row">Phrase [3]</th><td><input name="my_options['phrases'][2]" value="" size="32" type="text"></td></tr>';
echo '</table>';
echo '</form>';
echo '</div>';
}
This allows the user to enter 3 phrases. The data validation declared in register_settings is:
function my_validate($input){
foreach ($input['phrases'] as $i => $phrase) {
$output['phrase'][$i] = esc_html($phrase);
}
return $output;
}
I am aware that I can add as many input fields as I need into the form to extend this from the 3 inputs, however, I would like the user to be able to add any number of phrases which then will be processed when the form is submitted and will be stored to the $output['phrase'][] array.
Is there some way to implement a multi-input field that will take as many phrases as the user wants to enter?
Any ideas or suggestions how to approach this will be welcomed.
input
fields appear?