Skip to content

Instantly share code, notes, and snippets.

@alexstandiford
Created November 14, 2016 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexstandiford/9130477f401c2b9c3a11993161eb88e2 to your computer and use it in GitHub Desktop.
Save alexstandiford/9130477f401c2b9c3a11993161eb88e2 to your computer and use it in GitHub Desktop.
WordPress Options, Simplified
class option{
public function __construct($ID,$title,$object,$callback = null,$page = null,$section = null,$prefix = 'your_prefix_'){
$this->ID = $prefix.$ID;
$this->title = $title;
$this->callback = ($callback == null) ? array($object,$this->ID.'_callback') : array($object,$callback);
$this->page = ($page == null) ? 'eav-settings-admin' : $page;
$this->section = ($section == null) ? 'eav_options_id' : $section;
}
}
class eavSettings{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct(){
add_action('admin_menu', array($this, 'add_plugin_page'));
add_action('admin_init', array($this, 'page_init'));
$this->options = array(
new option('option_id','Option Description',$this),
);
}
/**
* Add options page
*/
public function add_plugin_page(){
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'Easy Age Verifier',
'manage_options',
'easy-age-verifier-settings',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page(){
// Set class property
$this->options = get_option( 'eav_options' );
?>
<h2>Options Title</h2>
<div class="wrapper">
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'eav_options_group' );
do_settings_sections( 'eav-settings-admin' );
submit_button();
?>
</form>
<div class="eav-admin-sidebar">
<?php do_action('eav_settings_sidebar');?>
</div>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init(){
register_setting(
'eav_options_group', // Option group
'eav_options' // Option name
);
add_settings_section(
'eav_options_id', // ID
'Easy Age Verifier Settings', // Title
array( $this, 'print_section_info' ), // Callback
'eav-settings-admin' // Page
);
//Loop through and build the options
foreach($this->options as $option){
add_settings_field(
$option->ID,
$option->title,
$option->callback,
$option->page,
$option->section
);
}
}
/**
* Print the Section text
*/
public function print_section_info(){
print 'Enter your settings below:';
}
/**
* Get the settings option array and print one of its values
*/
public function callback_example(){
printf(
'<input type="number" id="eav_minimum_age" name="eav_options[eav_minimum_age]" value="%s" />',
$this->options['eav_minimum_age'] != '' && $this->options['eav_minimum_age'] != 0 ? esc_attr( $this->options['eav_minimum_age']) : apply_filters('eav_default_age',21)
);
}
}
if(is_admin()){
$eav_settings = new settings();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment