+ * Plugin Name: Settings API Option Class
+ * Plugin URI: http://www.slimbobwp.com/plugins/settings-api-option-class/
+ * Description: Settings API class to build options pages easily.
+ * Author URI: http://slimbobwp.com
+class settings_api_option {
+ function __construct( $data ) {
+ add_action( 'admin_init', array( $this, 'register_the_settings' ) );
+ add_action( 'admin_init', array( $this, 'delete_orphan_settings' ) );
+ add_action( 'admin_menu', array( $this, 'register_settings_menu_link' ) );
+ function register_the_settings() {
+ register_setting( $this->data['menu_slug'], $this->data['option_name'], array( $this, 'sanitize_setting' ) );
+ foreach( $this->data['sections'] as $section ) {
+ add_settings_section( $section['slug'], $section['title'], array( $this, 'settings_section_callback' ), $this->data['menu_slug'] );
+ foreach( $this->data['fields'] as $field ) {
+ 'title' => $field['title'],
+ 'type' => $field['type'],
+ 'choices' => $field['choices'],
+ 'description' => $field['description'],
+ if( 'radio' != $field['type'] && 'multicheck' != $field['type'] ) {
+ $args['label_for'] = $this->data['option_name'] . '[' . $field['id'] . ']';
+ add_settings_field( $args['id'], $args['title'], array( $this, 'settings_field_callback' ), $this->data['menu_slug'], $field['section'], $args );
+ function sanitize_setting( $setting ) {
+ foreach( $this->data['fields'] as $field ) {
+ if( ! isset( $setting[ $field['id'] ] ) && 'multicheck' != $field['type'] ) {
+ $setting[ $field['id'] ] = '';
+ if( 'multicheck' == $field['type'] ) {
+ if( ! isset( $setting[ $field['id'] ] ) ) {
+ $setting[ $field['id'] ] = array();
+ foreach( $field['choices'] as $choice_value => $choice_title ) {
+ if( ! isset( $setting[ $field['id'] ][ $choice_value ] ) ) {
+ $setting[ $field['id'] ][ $choice_value ] = '';
+ function delete_orphan_settings() {
+ $option = get_option( $this->data['option_name'] );
+ foreach( $option as $option_slug => $option_value ) {
+ $option_found = 'false';
+ foreach( $this->data['fields'] as $field ) {
+ if( $field['id'] == $option_slug ) {
+ $option_found = 'true';
+ if( is_array( $option_value ) && 'multicheck' == $field['type'] ) {
+ foreach( $option_value as $multicheck_option_slug => $multicheck_option_value ) {
+ $multicheck_option_found = 'false';
+ foreach( $field['choices'] as $choice_value => $choice_title ) {
+ if( $choice_value == $multicheck_option_slug ) {
+ $multicheck_option_found = 'true';
+ if( 'false' == $multicheck_option_found ) {
+ unset( $option[ $option_slug ][ $multicheck_option_slug ] );
+ update_option( $this->data['option_name'], $option );
+ if( 'false' == $option_found ) {
+ unset( $option[ $option_slug ] );
+ update_option( $this->data['option_name'], $option );
+ function settings_section_callback( $arg ) {
+ foreach( $this->data['sections'] as $section ) {
+ if( $section['slug'] == $arg['id'] ) {
+ if( isset( $section['description'] ) && '' != $section['description'] ) {
+ echo '<p class="description">' . $section['description'] . '</p>';
+ function settings_field_callback( $args ) {
+ $option = get_option( $this->data['option_name'] );
+ switch( $args['type'] ) {
+ echo '<input type="text" class="regular-text" name="' . $this->data['option_name'] . '[' . $args['id'] . ']" id="' . $this->data['option_name'] . '[' . $args['id'] . ']" value="' . $option[ $args['id'] ] . '" />';
+ echo '<select name="' . $this->data['option_name'] . '[' . $args['id'] . ']" id="' . $this->data['option_name'] . '[' . $args['id'] . ']">';
+ foreach( $args['choices'] as $choice_value => $choice_title ) {
+ echo '<option value="' . $choice_value . '" ' . selected( $option[ $args['id'] ], $choice_value ) . '>' . $choice_title . '</option>';
+ foreach( $args['choices'] as $choice_value => $choice_title ) {
+ echo '<input type="radio" name="' . $this->data['option_name'] . '[' . $args['id'] . ']" id="' . $choice_value . '" ' . checked( $option[ $args['id'] ], $choice_value, false ) . ' value="' . $choice_value . '" /> ';
+ echo '<label for="' . $choice_value . '">' . $choice_title . '</label><br />';
+ echo '<input type="checkbox" name="' . $this->data['option_name'] . '[' . $args['id'] . ']" id="' . $this->data['option_name'] . '[' . $args['id'] . ']" ' . checked( $option[ $args['id'] ], 1, false ) . ' value="1" /> ';
+ echo '<label for="' . $this->data['option_name'] . '[' . $args['id'] . ']">' . $args['title'] . '</label>';
+ foreach( $args['choices'] as $choice_value => $choice_title ) {
+ echo '<input type="checkbox" name="' . $this->data['option_name'] . '[' . $args['id'] . '][' . $choice_value . ']" id="' . $this->data['option_name'] . '[' . $args['id'] . '][' . $choice_value . ']" ' . checked( $option[ $args['id'] ][ $choice_value ], 1, false ) . ' value="1" /> ';
+ echo '<label for="' . $this->data['option_name'] . '[' . $args['id'] . '][' . $choice_value . ']">' . $choice_title . '</label><br />';
+ echo '<textarea rows="10" cols="50" class="large-text" name="' . $this->data['option_name'] . '[' . $args['id'] . ']" id="' . $this->data['option_name'] . '[' . $args['id'] . ']">' . $option[ $args['id'] ] . '</textarea>';
+ if( isset( $args['description'] ) && '' != $args['description'] ) {
+ echo '<p class="description">' . $args['description'] . '</p>';
+ function register_settings_menu_link() {
+ $function = array( $this, 'output_admin_page' );
+ add_options_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], $function);
+ function output_admin_page() {
+ echo '<div class="wrap">';
+ echo '<h1>' . $this->data['page_title'] . '</h1>';
+ // Uncomment the following function to see output of option array.
+ //$this->pretty_option_dump();
+ echo '<form method="post" action="options.php">';
+ settings_fields( $this->data['menu_slug'] );
+ do_settings_sections( $this->data['menu_slug'] );
+ submit_button( $this->data['submit_button_text'] );
+ function pretty_option_dump() {
+ var_dump( get_option( $this->data['option_name'] ) );
+ 'page_title' => 'Settings API Test',
+ 'menu_title' => 'Settings API Test',
+ 'capability' => 'manage_options',
+ 'menu_slug' => 'settings_api_test',
+ 'option_name' => 'settings_api_test_settings',
+ 'slug' => 'settings_api_test_settings_text_section',
+ 'title' => 'Text Input Test Section',
+ 'description' => 'This is a sample section description for our text input test.',
+ 'slug' => 'settings_api_test_settings_select_section',
+ 'title' => 'Select Input Test Section',
+ 'description' => 'This is another sample section description for our select input test.',
+ 'slug' => 'settings_api_test_settings_radio_section',
+ 'title' => 'Radio Input Test Section',
+ 'description' => 'This is another sample section description for our radio input test.',
+ 'slug' => 'settings_api_test_settings_checkbox_section',
+ 'title' => 'Checkbox Input Test Section',
+ 'description' => 'This is another sample section description for our checkbox input test.',
+ 'slug' => 'settings_api_test_settings_multicheck_section',
+ 'title' => 'Multicheck Input Test Section',
+ 'description' => 'This is another sample section description for our multicheck input test.',
+ 'slug' => 'settings_api_test_settings_textarea_section',
+ 'title' => 'Textarea Input Test Section',
+ 'description' => 'This is another sample section description for our textarea input test.',
+ 'title' => 'Test Text',
+ 'section' => 'settings_api_test_settings_text_section',
+ 'description' => 'This is a sample field description for our text input test.',
+ 'title' => 'Test Dropdown',
+ 'section' => 'settings_api_test_settings_select_section',
+ 'description' => 'This is another sample field description for our select input test.',
+ 'value_one' => 'Value One',
+ 'value_two' => 'Value Two',
+ 'value_three' => 'Value Three',
+ 'value_four' => 'Value Four',
+ 'default' => 'value_three',
+ 'title' => 'Test Radio',
+ 'section' => 'settings_api_test_settings_radio_section',
+ 'description' => 'This is another sample field description for our radio input test.',
+ 'value_one' => 'Value One',
+ 'value_two' => 'Value Two',
+ 'value_three' => 'Value Three',
+ 'value_four' => 'Value Four',
+ 'default' => 'value_four',
+ 'id' => 'test_checkbox',
+ 'title' => 'Test Checkbox',
+ 'section' => 'settings_api_test_settings_checkbox_section',
+ 'description' => 'This is another sample field description for our checkbox input test.',
+ 'id' => 'test_multicheck',
+ 'title' => 'Test Multicheck',
+ 'section' => 'settings_api_test_settings_multicheck_section',
+ 'description' => 'This is another sample field description for our multicheck input test.',
+ 'type' => 'multicheck',
+ 'value_one' => 'Value One',
+ 'value_two' => 'Value Two',
+ 'value_three' => 'Value Three',
+ 'value_four' => 'Value Four',
+ 'default' => 'value_three',
+ 'id' => 'test_textarea',
+ 'title' => 'Test Textarea',
+ 'section' => 'settings_api_test_settings_textarea_section',
+ 'description' => 'This is a sample field description for our textarea input test.',
+ 'submit_button_text' => 'Save Changes',
+$settings_api_option = new settings_api_option( $data );