Before adding the select list itself, we would first add a line to initialize the select_list option in the ch3sapi_get_options function, if not found in the options retrieved from the database: $new_options['select_list'] = 'First';
The next step to rendering a drop-down list is to provide the list of all possible options, along with the option name, in the optional field data array. Here is an example of a call to the add_settings_field function call with such a list:
add_settings_field( 'select_list', 'Select List',
'ch3sapi_select_list', 'ch3sapi_settings_section',
'ch3sapi_main_section',
array( 'name' => 'select_list',
'choices' => array( 'First', 'Second', 'Third' ) ) );
With this information, we can provide an implementation for the ch3sapi_select_list function that will be able to render an HTML select element using the choices array to populate it:
function ch3sapi_select_list( $data = array() ) {
extract ( $data );
$options = ch3sapi_get_options();
?>
<select name="ch3sapi_options[<?php echo $name; ?>]">
<?php foreach( $choices as $item ) { ?>
<option value="<?php echo $item; ?>"
<?php selected( $options[$name] == $item ); ?>>
<?php echo $item; ?></option>;
<?php } ?>
</select>
<?php }