The usual settings implemented within plugin files. Here just to speed things up. Implement.
Add $this->sections = $this->get_sections(); to __construct, and define add_action('admin_init' [$this, 'register_settings']);
| /** | |
| * Register each of the setting groups. | |
| * @since 1.0.0 | |
| */ | |
| public function register_settings() | |
| { | |
| foreach( $this->sections as $section ) { | |
| add_settings_section( $section['id'], $section['title'], $section['callback'], $section['page'] ); | |
| } | |
| $fields = $this->get_fields(); | |
| foreach( $fields as $field ) { | |
| $this->create_field( $field ); | |
| } | |
| foreach( $this->sections as $section ) { | |
| register_setting( $section['page'], $section['page'], [$this, 'validate_options'] ); | |
| } | |
| } | |
| /** | |
| * Render each section description in p tags. | |
| * @since 1.0.0 | |
| */ | |
| public function render_section( $desc ) | |
| { | |
| echo "<p></p>"; | |
| } | |
| /** | |
| * Geat each of the sections for the admin page. | |
| * @return array $sections | |
| */ | |
| public function get_sections() | |
| { | |
| $sections = []; | |
| $sections[$this->prefix . 'general'] = [ | |
| 'id' => $this->prefix . 'general', | |
| 'page' => $this->prefix . 'general_page', | |
| 'title' => 'General', | |
| 'callback' => [$this, 'render_section'], | |
| ]; | |
| return $sections; | |
| } | |
| /** | |
| * set the fields for the sections. | |
| * @return array $fields | |
| */ | |
| public function get_fields() | |
| { | |
| $fields = []; | |
| # General | |
| $fields[] = [ | |
| 'id' => $this->prefix . 'post_count', | |
| 'title' => 'Text Field', | |
| 'callback' => [$this, 'render_field'], | |
| 'page' => $this->prefix . 'general_page', | |
| 'section' => $this->prefix . 'general', | |
| 'desc' => 'description.', | |
| 'type' => 'text', | |
| 'default_value' => '', | |
| 'class' => '' | |
| ]; | |
| return $fields; | |
| } | |
| /** | |
| * create the fields from the data we define above | |
| * @param array $field | |
| */ | |
| public function create_field($field) | |
| { | |
| extract ($field); | |
| $field_args = [ | |
| 'id' => $id, | |
| 'page' => $page, | |
| 'type' => $type, | |
| 'desc' => $desc, | |
| 'default_value' => $default_value, | |
| 'class' => $class | |
| ]; | |
| add_settings_field( $id, $title, [$this, 'render_field'], $page, $section, $field_args ); | |
| } | |
| /** | |
| * set the formatting on the back-end for each of the field | |
| */ | |
| public function render_field ($field_args = []) | |
| { | |
| extract( $field_args ); | |
| $options = get_option( $page ); | |
| $html = '<div class="' . $class . '">'; | |
| switch ($type) { | |
| case 'text': | |
| $value = isset($options[$id]) ? $options[$id] : ''; | |
| $html .= '<input type="text" id="' . $id . '" name="' . $page . '[' . $id . ']" value="' . $value . '" />'; | |
| $html .= '<br/><span class="field-desc">' . $desc . '</span>'; | |
| break; | |
| case 'number': | |
| $value = isset($options[$id]) ? $options[$id] : ''; | |
| $html .= '<input type="number" id="' . $id . '" name="' . $page . '[' . $id . ']" value="' . $value . '" />'; | |
| $html .= '<br/><span class="field-desc">' . $desc . '</span>'; | |
| break; | |
| case 'checkbox': | |
| $html .= '<input type="checkbox" id="' . $id . '" name="' . $page . '[' . $id . ']" value="1" ' . checked (1, isset ($options[$id]) ? $options[$id] : 0, false) . '/>'; | |
| $html .= '<label for="' . $id . '"> ' . $desc . '</label>'; | |
| break; | |
| default: | |
| break; | |
| } | |
| $html .= '</div>'; | |
| echo $html; | |
| } | |
| /** | |
| * validate our inputs | |
| */ | |
| public function validate_options($input) | |
| { | |
| $output = []; | |
| foreach ( $input as $key => $value ) { | |
| if ( isset( $input[$key] ) ) | |
| $output[$key] = strip_tags( stripslashes( $input[$key] ) ); | |
| } | |
| return apply_filters('validate_options', $output, $input); | |
| } |