Last active
December 22, 2015 19:27
-
-
Save patrickodacre/fd1482fd64d09e6f00ad to your computer and use it in GitHub Desktop.
Adding metaboxes to an options page using the settings API. Config file is injected to create new fields, choose their metaboxes and sanitization callbacks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php namespace POD_Core\Admin; | |
| /** | |
| * class-options-metabox.php | |
| * | |
| * @package POD Core Functionality | |
| * @author Patrick O'Dacre | |
| * @link http://patrickwho.me | |
| * @copyright 2015 Patrick O'Dacre | |
| * @license GPL-2.0+ | |
| */ | |
| use POD_Core\Support; | |
| class OptionsMetabox { | |
| protected $metabox = array(); | |
| protected $settings = array(); | |
| protected $field_sanitization_list = array(); | |
| public function __construct( $config ) { | |
| $this->settings = $config['main-settings']; | |
| $this->metabox = $config['metabox_sections']; | |
| $this->field_sanitization_list = $this->build_sanitization_list(); | |
| } | |
| /** | |
| * Read config arrays for option sections and fields. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| public function init() { | |
| $this->register_option_group_and_db_name(); | |
| $this->walk_section_config_array(); | |
| $this->walk_fields_config_array(); | |
| } | |
| /** | |
| * Register the option group and the database option_name. | |
| * | |
| * @since 1.0.0 | |
| * | |
| * @link https://codex.wordpress.org/Function_Reference/register_setting | |
| */ | |
| protected function register_option_group_and_db_name() { | |
| register_setting( | |
| $this->settings['option-group'], | |
| $this->settings['option-name'], | |
| array( $this, 'sanitize_all_the_things' ) | |
| ); | |
| } | |
| /** | |
| * Sanitize option field input value. | |
| * | |
| * Method will use core sanitization callbacks if found. | |
| * If core callback not found, method will look for a new method | |
| * within this class. | |
| * | |
| * @since 1.0.0 | |
| * | |
| * @param array $option_value_array Array of option key and value pairs. | |
| * | |
| * @return array $option_value_array Array with sanitized input values. | |
| */ | |
| public function sanitize_all_the_things( $option_value_array ) { | |
| foreach( $option_value_array as $option_name => $val ) { | |
| $sanitize_function = $this->field_sanitization_list[$option_name]; | |
| if( function_exists( $this->field_sanitization_list[$option_name] ) ) { | |
| $new_val = $sanitize_function( $val ); | |
| } else { | |
| $new_val = $this->$sanitize_function( $val ); | |
| } | |
| $option_value_array[$option_name] = $new_val; | |
| } | |
| return $option_value_array; | |
| } | |
| /** | |
| * Look through all the sections in the config file. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| protected function walk_section_config_array() { | |
| array_walk( $this->metabox, array( $this, 'add_section' ) ); | |
| } | |
| /** | |
| * Create option sections. | |
| * | |
| * @since 1.0.0 | |
| * | |
| * @param $section | |
| * | |
| * @link https://codex.wordpress.org/Function_Reference/add_settings_section | |
| */ | |
| protected function add_section( $section ) { | |
| /** | |
| * Output custom description for each section. | |
| * | |
| * @since 1.0.0 | |
| * | |
| * @param array $section Section config array. | |
| */ | |
| $callback = function() use ( $section ) { | |
| echo '<p>' . $section['args']['desc'] . '</p>'; | |
| }; | |
| add_settings_section( | |
| $section['args']['id'], | |
| $section['args']['title'], | |
| $callback, | |
| $section['args']['page-slug'] | |
| ); | |
| } | |
| /** | |
| * Walk through each option section to find fields. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| protected function walk_fields_config_array() { | |
| array_walk( $this->metabox, array( $this, 'walk_fields_to_add_fields' ) ); | |
| } | |
| /** | |
| * Walk each field found in each section. | |
| * | |
| * @since 1.0.0 | |
| * | |
| * @param array $section Section config array. | |
| */ | |
| protected function walk_fields_to_add_fields( $section ) { | |
| array_walk( $section['fields'], array( $this, 'add_fields' ) ); | |
| } | |
| /** | |
| * Add each field to their sections. | |
| * | |
| * @since 1.0.0 | |
| * | |
| * @param array $field Option fields. | |
| */ | |
| protected function add_fields( $field ) { | |
| // Grab options to use in the view files. | |
| $options = get_option( $this->settings['option-name'] ); | |
| // Include the appropriate view file. | |
| $view = $this->settings['views'][$field['type']]; | |
| /** | |
| * Render the field view using info from config file. | |
| * | |
| * @since 1.0.0 | |
| * | |
| * @param array $field Field type and id. | |
| * @param array $options Options saved in db from options page. | |
| */ | |
| $callback = function() use ( $field, $options, $view ) { | |
| $this->render_view( $field, $options, $view ); | |
| }; | |
| add_settings_field( | |
| $field['id'], | |
| $field['title'], | |
| $callback, | |
| $field['page-slug'], | |
| $field['section'] | |
| ); | |
| } | |
| /** | |
| * Render option field metabox. | |
| * | |
| * @param array $field Field config array. | |
| * @param array $options Custom option field in db. | |
| * @param mixed $view View file name with extension. | |
| */ | |
| protected function render_view( $field, $options, $view ) { | |
| include( POD_CORE_PLUGIN_DIR . 'src/views/metaboxes/' . $view ); | |
| } | |
| /** | |
| * Build an array of all field keys and their respective sanitization callbacks. | |
| * | |
| * @since 1.0.0 | |
| * | |
| * @return array $list Array of option keys and their associated callbacks. | |
| */ | |
| protected function build_sanitization_list() { | |
| $list = array(); | |
| foreach( $this->metabox as $metabox ) { | |
| foreach( $metabox['fields'] as $field ) { | |
| $list[$field['key']] = $field['callback']; | |
| } | |
| } | |
| return $list; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $page_slug = 'podc-settings'; | |
| $option_name = 'podc_settings'; | |
| return array( | |
| // Be sure to do a search and replace when changing any of these values. | |
| 'main-settings' => array( | |
| 'heading' => 'Core Site Options', | |
| 'menu-title' => 'Site Options', | |
| 'page-slug' => $page_slug, | |
| 'dashicon' => 'dashicons-palmtree', | |
| 'option-group' => $option_name.'_group', | |
| 'option-name' => $option_name, | |
| 'views' => array( | |
| 'text-small' => 'text-small.php', | |
| 'text-medium' => 'text-medium.php', | |
| 'text-large' => 'text-large.php', | |
| 'text-area' => 'text-area.php', | |
| 'checkbox' => 'checkbox.php', | |
| 'radio' => 'radio.php', | |
| 'options' => 'options.php', | |
| ), | |
| ), | |
| 'metabox_sections' => array( | |
| 'section_one' => array( | |
| 'args' => array( | |
| 'id' => 'section_business', | |
| 'title' => 'Business Information', | |
| 'desc' => 'Complete at least all required fields.', | |
| 'page-slug' => $page_slug, | |
| ), | |
| 'fields' => array( | |
| 'business_name' => array( | |
| 'type' => 'text-small', | |
| 'id' => $option_name.'[business_name]', | |
| 'title' => 'Business Name', | |
| 'desc' => '', | |
| 'page-slug' => $page_slug, | |
| 'section' => 'section_business', | |
| 'key' => 'business_name', | |
| 'required' => true, | |
| 'callback' => 'sanitize_text_field', | |
| ), | |
| 'business_address' => array( | |
| 'type' => 'text-large', | |
| 'id' => $option_name.'[business_address]', | |
| 'title' => 'Business Address', | |
| 'desc' => 'ie: 123 Any St, Suite 101', | |
| 'page-slug' => $page_slug, | |
| 'section' => 'section_business', | |
| 'key' => 'business_address', | |
| 'required' => true, | |
| 'callback' => 'sanitize_text_field', | |
| ), | |
| ), // end all fields | |
| ), // end section one | |
| 'section_two' => array( | |
| 'args' => array( | |
| 'id' => 'business_hours', | |
| 'title' => 'Business Hours', | |
| 'desc' => 'Fill out business hours like so: "9:00 am - 5:00 pm" OR "Closed", as the case may be.', | |
| 'page-slug' => $page_slug, | |
| ), | |
| 'fields' => array( | |
| 'hours_monday' => array( | |
| 'type' => 'text-small', | |
| 'id' => $option_name.'[hours_monday]', | |
| 'title' => 'Monday Hours', | |
| 'desc' => '', | |
| 'page-slug' => $page_slug, | |
| 'section' => 'business_hours', | |
| 'key' => 'hours_monday', | |
| 'required' => false, | |
| 'callback' => 'sanitize_text_field', | |
| ), | |
| 'hours_tuesday' => array( | |
| 'type' => 'text-small', | |
| 'id' => $option_name.'[hours_tuesday]', | |
| 'title' => 'Tuesday Hours', | |
| 'desc' => '', | |
| 'page-slug' => $page_slug, | |
| 'section' => 'business_hours', | |
| 'key' => 'hours_tuesday', | |
| 'required' => false, | |
| 'callback' => 'sanitize_text_field', | |
| ), | |
| 'hours_wednesday' => array( | |
| 'type' => 'text-small', | |
| 'id' => $option_name.'[hours_wednesday]', | |
| 'title' => 'Wednesday Hours', | |
| 'desc' => '', | |
| 'page-slug' => $page_slug, | |
| 'section' => 'business_hours', | |
| 'key' => 'hours_wednesday', | |
| 'required' => false, | |
| 'callback' => 'sanitize_text_field', | |
| ), | |
| 'hours_thursday' => array( | |
| 'type' => 'text-small', | |
| 'id' => $option_name.'[hours_thursday]', | |
| 'title' => 'Thursday Hours', | |
| 'desc' => '', | |
| 'page-slug' => $page_slug, | |
| 'section' => 'business_hours', | |
| 'key' => 'hours_thursday', | |
| 'required' => false, | |
| 'callback' => 'sanitize_text_field', | |
| ), | |
| 'hours_friday' => array( | |
| 'type' => 'text-small', | |
| 'id' => $option_name.'[hours_friday]', | |
| 'title' => 'Friday Hours', | |
| 'desc' => '', | |
| 'page-slug' => $page_slug, | |
| 'section' => 'business_hours', | |
| 'key' => 'hours_friday', | |
| 'required' => false, | |
| 'callback' => 'sanitize_text_field', | |
| ), | |
| 'hours_saturday' => array( | |
| 'type' => 'text-small', | |
| 'id' => $option_name.'[hours_saturday]', | |
| 'title' => 'Saturday Hours', | |
| 'desc' => '', | |
| 'page-slug' => $page_slug, | |
| 'section' => 'business_hours', | |
| 'key' => 'hours_saturday', | |
| 'required' => false, | |
| 'callback' => 'sanitize_text_field', | |
| ), | |
| 'hours_sunday' => array( | |
| 'type' => 'text-small', | |
| 'id' => $option_name.'[hours_sunday]', | |
| 'title' => 'Sunday Hours', | |
| 'desc' => '', | |
| 'page-slug' => $page_slug, | |
| 'section' => 'business_hours', | |
| 'key' => 'hours_sunday', | |
| 'required' => false, | |
| 'callback' => 'sanitize_text_field', | |
| ), | |
| ), // end all fields | |
| ), // End section two | |
| ), // End metabox sections | |
| ); // end config array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * View file for a textarea field. | |
| * | |
| * @package POD Core Functionality | |
| * @author Patrick O'Dacre | |
| * @link http://patrickwho.me | |
| * @copyright 2015 Patrick O'Dacre | |
| * @license GPL-2.0+ | |
| */ | |
| ?> | |
| <p><label for="<?php echo $field['id'] ?>"><?php echo $field['desc'] ?></label></p> | |
| <p> | |
| <textarea rows="5" cols="50" name="<?php echo $field['id'] ?>" id="<?php echo $field['id'] ?>"><?php echo $options[$field['key']] ?></textarea> | |
| </p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * View file for a large text field. | |
| * | |
| * @package POD Core Functionality | |
| * @author Patrick O'Dacre | |
| * @link http://patrickwho.me | |
| * @copyright 2015 Patrick O'Dacre | |
| * @license GPL-2.0+ | |
| */ | |
| ?> | |
| <p><label for="<?php echo $field['id'] ?>"><?php echo $field['desc'] ?></label></p> | |
| <p> | |
| <input size="70" type="text" name="<?php echo $field['id'] ?>" id="<?php echo $field['id'] ?>" value="<?php echo $options[$field['key']] ?>"> | |
| </p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment