Skip to content

Commit

Permalink
default options refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
tareq1988 committed Jun 14, 2012
1 parent afb03db commit 0f44e19
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions class.settings-api.php
Expand Up @@ -70,14 +70,6 @@ function set_fields( $fields ) {
}

function add_field( $section, $field ) {
$defaults = array(
'name' => '',
'label' => '',
'desc' => '',
'type' => 'text'
);

$arg = wp_parse_args( $field, $defaults );
$this->settings_fields[$section][] = $arg;
}

Expand All @@ -103,16 +95,7 @@ function admin_init() {
//register settings fields
foreach ($this->settings_fields as $section => $field) {
foreach ($field as $option) {
$args = array(
'id' => $option['name'],
'desc' => $option['desc'],
'name' => $option['label'],
'section' => $section,
'size' => isset( $option['size'] ) ? $option['size'] : null,
'options' => isset( $option['options'] ) ? $option['options'] : '',
'std' => isset( $option['default'] ) ? $option['default'] : ''
);
add_settings_field( $section . '[' . $option['name'] . ']', $option['label'], array($this, 'callback_' . $option['type']), $section, $section, $args );
$this->init_field( $section, $option );
}
}

Expand All @@ -122,6 +105,37 @@ function admin_init() {
}
}

/**
* Register a single settings field
*
* @param string $section section name
* @param array $field section field option
*/
function init_field( $section, $field ) {
$defaults = array(
'name' => '',
'label' => '',
'desc' => '',
'type' => 'text',
'default' => '',
'options' => '',
'size' => ''
);

$option = wp_parse_args( $field, $defaults );

$args = array(
'id' => $option['name'],
'desc' => $option['desc'],
'name' => $option['label'],
'section' => $section,
'size' => $option['size'],
'options' => $option['options'],
'std' => $option['default']
);
add_settings_field( $section . '[' . $option['name'] . ']', $option['label'], array($this, 'callback_' . $option['type']), $section, $section, $args );
}

/**
* Displays a text field for a settings field
*
Expand Down

0 comments on commit 0f44e19

Please sign in to comment.