Last active
September 2, 2019 21:47
Revisions
-
JSila revised this gist
Apr 27, 2017 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,12 @@ $settings = new WP_Settings('menu-page-slug'); // adding a section $settings->add_section('Section Title', 'section_html_callback') ->add_field('settings-field-1', 'Settings Field 1', 'settings_field_1_html_callback') ->add_field('settings-field-2', 'Settings Field 2', 'settings_field_2_html_callback'); // adding another section (i used the same data only to show you how you can add another section) $settings->add_section('Section Title', 'section_html_callback') ->add_field('settings-field-1', 'Settings Field 1', 'settings_field_1_html_callback') ->add_field('settings-field-2', 'Settings Field 2', 'settings_field_2_html_callback'); -
JSila revised this gist
Apr 27, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -45,7 +45,7 @@ class WP_Settings { public function __construct($page_id) { $this->page_id = $page_id; $this->options_group = $page_id; } public function add_section($title = null, $output_callback = null) { -
JSila revised this gist
Apr 27, 2017 . 2 changed files with 3 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -67,8 +67,8 @@ public function init() { $field_id, $field['title'], $field['output_callback'], $this->page_id, $section_id ); register_setting($this->options_group, $field_id); } 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 charactersOriginal file line number Diff line number Diff line change @@ -10,6 +10,7 @@ function section_html_callback() {} function settings_field_1_html_callback() {} function settings_field_2_html_callback() {} // other code for creating menu page omitted except the one where Settings API would normally be used. function menu_page_html_output() { ?> <div class="wrap"> -
JSila created this gist
Apr 27, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,83 @@ <?php class WP_Settings_Section { protected $id = ''; protected $title = ''; protected $output_callback = ''; protected $fields = array(); public function __construct($id, $title, $output_callback) { $this->id = $id; $this->title = $title; $this->output_callback = $output_callback; } public function add_field($id, $title, $output_callback) { $this->fields[$id] = array( 'id' => $id, 'title' => $title, 'output_callback' => $output_callback ); return $this; } public function get_id() { return $this->id; } public function get_title() { return $this->title; } public function get_output_callback() { return $this->output_callback; } public function get_fields() { return $this->fields; } } class WP_Settings { protected $page_id = ''; protected $sections = array(); protected $options_group = ''; public function __construct($page_id) { $this->page_id = $page_id; $this->options_group = uniqid(); } public function add_section($title = null, $output_callback = null) { $section_id = uniqid(); $section = new WP_Settings_Section($section_id, $title, $output_callback); $this->sections[$section_id] = $section; return $section; } public function init() { foreach($this->sections as $section_id => $section) { add_settings_section( $section_id, $section->get_title(), $section->get_output_callback(), $this->page_id ); foreach($section->get_fields() as $field_id => $field) { add_settings_field( $field_id, $field['title'], $field['output_callback'], $section_id, $this->page_id ); register_setting($this->options_group, $field_id); } } } public function do_sections() { do_settings_sections($this->page_id); } public function fields() { settings_fields($this->options_group); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ <?php $settings = new WP_Settings('menu-page-slug'); $settings->add_section('Section Title', 'section_html_callback') ->add_field('settings-field-1', 'Settings Field 1', 'settings_field_1_html_callback') ->add_field('settings-field-2', 'Settings Field 2', 'settings_field_2_html_callback'); function section_html_callback() {} function settings_field_1_html_callback() {} function settings_field_2_html_callback() {} function menu_page_html_output() { ?> <div class="wrap"> <form method="POST" action="options.php"> <?php global $settings; $settings->do_sections(); $settings->fields(); submit_button(); ?> </form> </div> <?php } add_action('admin_init', array($settings, 'init'));