Skip to content

Instantly share code, notes, and snippets.

@JSila
Last active September 2, 2019 21:47
Show Gist options
  • Save JSila/44cf1d90e6f822aedfd8e91389c00910 to your computer and use it in GitHub Desktop.
Save JSila/44cf1d90e6f822aedfd8e91389c00910 to your computer and use it in GitHub Desktop.
WP Settings API in OOP way
<?php
$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');
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">
<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'));
<?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 = $page_id;
}
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'],
$this->page_id,
$section_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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment