Well you could do something like this:
Create an associative array for settings, then loop through them to create settings and sections for each field.
$settings = array(
'setting_1_id' => array(
'title'=>'First Box Settings',
'page'=>'first_box_option',
'fields'=> array(
array(
'id'=> 'box_first_title',
'title'=>'Title',
'callback'=> 'text_callback'
),
array(
'id'=> 'box_first_desc',
'title'=>'Description',
'callback'=> 'textarea_callback'
),
array(
'id'=> 'box_first_link',
'title'=>'Link',
'callback'=> 'text_callback'
),
)
),
'setting_2_id' => array(
'title'=>'Second Box Settings',
'page'=>'second_box_option',
'fields'=> array(
array(
'id'=> 'box_second_title',
'title'=>'Title',
'callback'=> 'text_callback'
),
array(
'id'=> 'box_second_desc',
'title'=>'Description',
'callback'=> 'textarea_callback'
),
array(
'id'=> 'box_second_link',
'title'=>'Link',
'callback'=> 'text_callback'
),
)
),
'setting_3_id' => array(
'title'=>'Third Box Settings',
'page'=>'third_box_option',
'fields'=> array(
array(
'id'=> 'box_third_title',
'title'=>'Title',
'callback'=> 'text_callback'
),
array(
'id'=> 'box_third_desc',
'title'=>'Description',
'callback'=> 'textarea_callback'
),
array(
'id'=> 'box_third_link',
'title'=>'Link',
'callback'=> 'text_callback'
),
)
),
);
Then you can use the foreach to loop through each settings.
foreach( $settings as $id => $values){
add_settings_section(
$id, // ID used to identify this section and with which to register options
$values['title'],
'boxes_front_page_callback', // Callback used to render the description of the section
$values['page'] // Page on which to add this section of options
);
// Loop through the fields to add different fields
foreach ($values['fields'] as $field) {
add_settings_field(
$field['id'], // ID used to identify the field throughout the theme
$field['title'], // The label to the left of the option interface element
$field['callback'],
$values['page'], // The page on which this option will be added
$id, // ID of the section
array(
$values['page'], //option name
$field['title'] //id
)
);
}
register_setting($values['page'], $values['page']);
}
And finally you only need three callbacks, for text, textarea, and frontpage
/*
Call Backs
*/
function boxes_front_page_callback() {
echo '<p>Lorem ipsum</p>';
}
function text_callback($args) {
$options = get_option($args[0]);
echo '<input type="text" class="regular-text" id="' . $args[1] . '" name="'. $args[0] .'[' . $args[1] . ']" value="' . $options['' . $args[1] . ''] . '"></input>';
}
function textarea_callback($args) {
$options = get_option($args[0]);
echo '<textarea rows="8" cols="50" class="large-text" id="' . $args[1] . '" name="'. $args[0] .'[' . $args[1] . ']">' . $options['' . $args[1] . ''] . '</textarea>';
}
I have already tested this, works as intended.
Click here to look at the gist of the full code