Snippets

SlimBob WP Settings API Class

Updated by SlimBob WP

File settings-api-class.php Modified

  • Ignore whitespace
  • Hide word diff
 
 $settings_api_option = new settings_api_option( $data );
 
+// Need to add ability to add settings page as top level page, or as a submenu page for any core menu pages or custom post type menu pages.
+
 ?>
Updated by SlimBob WP

File settings-api-class.php Modified

  • Ignore whitespace
  • Hide word diff
 
 		array(
 
-			'id'                =>    'test_checkbox',
+			'id'                =>    'test_checkbox_a',
 
-			'title'             =>    'Test Checkbox',
+			'title'             =>    'Test Checkbox A',
+
+			'section'           =>    'settings_api_test_settings_checkbox_section',
+
+			'description'       =>    'This is another sample field description for our checkbox input test.',
+
+			'type'              =>    'checkbox',
+
+			'choices'           =>    '',
+
+			'default'           =>    1,
+
+		),
+
+		array(
+
+			'id'                =>    'test_checkbox_b',
+
+			'title'             =>    'Test Checkbox B',
 
 			'section'           =>    'settings_api_test_settings_checkbox_section',
 
Updated by SlimBob WP

File settings-api-class.php Modified

  • Ignore whitespace
  • Hide word diff
 
 	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 );
+		add_options_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 	}
 
Updated by SlimBob WP

File settings-api-class.php Modified

  • Ignore whitespace
  • Hide word diff
 
 		$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);
+		add_options_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], $function );
 
 	}
 
Created by SlimBob WP

File settings-api-class.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+/**
+ * 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.
+ * Version: 0.1
+ * Author: Bob Whitis
+ * Author URI: http://slimbobwp.com
+ * License: GPL2
+ */
+
+class settings_api_option {
+
+	var $data;
+
+	function __construct( $data ) {
+
+		$this->data = $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 ) {
+
+			$args = array(
+
+				'id'             =>    $field['id'],
+
+				'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 ] = '';
+
+					}
+
+				}
+
+			}
+
+		}
+
+		return $setting;
+
+	}
+
+	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'] ) {
+
+			case 'text':
+
+				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'] ] . '" />';
+
+				break;
+
+			case 'select':
+
+				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>';
+
+					}
+
+				echo '</select>';
+
+				break;
+
+			case 'radio':
+
+				echo '<fieldset>';
+
+					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 '</fieldset>';
+
+				break;
+
+			case 'checkbox':
+
+				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>';
+
+				break;
+
+			case 'multicheck':
+
+				echo '<fieldset>';
+
+					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 '</fieldset>';
+
+				break;
+
+			case 'textarea':
+
+				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>';
+
+				break;
+
+		}
+
+		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'] );
+
+			echo '</form>';
+
+		echo '</div>';
+
+	}
+
+	function pretty_option_dump() {
+
+		echo '<pre>';
+
+			var_dump( get_option( $this->data['option_name'] ) );
+
+		echo '</pre>';
+		
+	}
+
+}
+
+// Sample data array
+$data = array(
+
+	'page_title'            =>    'Settings API Test',
+
+	'menu_title'            =>    'Settings API Test',
+
+	'capability'            =>    'manage_options',
+
+	'menu_slug'             =>    'settings_api_test',
+
+	'option_name'           =>    'settings_api_test_settings',
+
+	'sections'              =>    array(
+
+		array(
+
+			'slug'              =>    'settings_api_test_settings_text_section',
+
+			'title'             =>    'Text Input Test Section',
+
+			'description'       =>    'This is a sample section description for our text input test.',
+
+		),
+
+		array(
+
+			'slug'              =>    'settings_api_test_settings_select_section',
+
+			'title'             =>    'Select Input Test Section',
+
+			'description'       =>    'This is another sample section description for our select input test.',
+
+		),
+
+		array(
+
+			'slug'              =>    'settings_api_test_settings_radio_section',
+
+			'title'             =>    'Radio Input Test Section',
+
+			'description'       =>    'This is another sample section description for our radio input test.',
+
+		),
+
+		array(
+
+			'slug'              =>    'settings_api_test_settings_checkbox_section',
+
+			'title'             =>    'Checkbox Input Test Section',
+
+			'description'       =>    'This is another sample section description for our checkbox input test.',
+
+		),
+
+		array(
+
+			'slug'              =>    'settings_api_test_settings_multicheck_section',
+
+			'title'             =>    'Multicheck Input Test Section',
+
+			'description'       =>    'This is another sample section description for our multicheck input test.',
+
+		),
+
+		array(
+
+			'slug'              =>    'settings_api_test_settings_textarea_section',
+
+			'title'             =>    'Textarea Input Test Section',
+
+			'description'       =>    'This is another sample section description for our textarea input test.',
+
+		),
+
+	),
+
+	'fields'                =>    array(
+
+		array(
+
+			'id'                =>    'test_text',
+
+			'title'             =>    'Test Text',
+
+			'section'           =>    'settings_api_test_settings_text_section',
+
+			'description'       =>    'This is a sample field description for our text input test.',
+
+			'type'              =>    'text',
+
+			'choices'           =>    '',
+
+			'default'           =>    '',
+
+		),
+
+		array(
+
+			'id'                =>    'test_select',
+
+			'title'             =>    'Test Dropdown',
+
+			'section'           =>    'settings_api_test_settings_select_section',
+
+			'description'       =>    'This is another sample field description for our select input test.',
+
+			'type'              =>    'select',
+
+			'choices'           =>    array(
+
+				'value_one'         =>    'Value One',
+
+				'value_two'         =>    'Value Two',
+
+				'value_three'       =>    'Value Three',
+
+				'value_four'        =>    'Value Four',
+
+			),
+
+			'default'           =>    'value_three',
+
+		),
+
+		array(
+
+			'id'                =>    'test_radio',
+
+			'title'             =>    'Test Radio',
+
+			'section'           =>    'settings_api_test_settings_radio_section',
+
+			'description'       =>    'This is another sample field description for our radio input test.',
+
+			'type'              =>    'radio',
+
+			'choices'           =>    array(
+
+				'value_one'         =>    'Value One',
+
+				'value_two'         =>    'Value Two',
+
+				'value_three'       =>    'Value Three',
+
+				'value_four'        =>    'Value Four',
+
+			),
+
+			'default'           =>    'value_four',
+
+		),
+
+		array(
+
+			'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.',
+
+			'type'              =>    'checkbox',
+
+			'choices'           =>    '',
+
+			'default'           =>    1,
+
+		),
+
+		array(
+
+			'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',
+
+			'choices'           =>    array(
+
+				'value_one'         =>    'Value One',
+
+				'value_two'         =>    'Value Two',
+
+				'value_three'       =>    'Value Three',
+
+				'value_four'        =>    'Value Four',
+
+			),
+
+			'default'           =>    'value_three',
+
+		),
+
+		array(
+
+			'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.',
+
+			'type'              =>    'textarea',
+
+			'choices'           =>    '',
+
+			'default'           =>    '',
+
+		),
+
+	),
+
+	'submit_button_text'    =>    'Save Changes',
+
+);
+
+$settings_api_option = new settings_api_option( $data );
+
+?>
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.