Skip to content

Instantly share code, notes, and snippets.

@amereservant
Last active December 18, 2015 11:59
Show Gist options
  • Save amereservant/5779754 to your computer and use it in GitHub Desktop.
Save amereservant/5779754 to your computer and use it in GitHub Desktop.
A simple Wordpress plugin/theme options class. This is basically from the Codex and I just modified it to use class properties to make customizing it a little simpler.
<?php
/**
* Plugin/Theme Options
*
* @link http://codex.wordpress.org/Creating_Options_Pages#Example_.232
* @link https://gist.github.com/amereservant/5779754
* @requires Wordpress 3.5.1 =<
*/
class wctest
{
private $_option_name = '_my_plugin_options';
private $_option_group = 'test_option_group';
private $_capability = 'manage_options';
private $_menu_slug = 'test-setting-admin';
private $_page_title = 'Settings Admin';
private $_menu_title = 'Premium Opts';
private $_section_id = 'setting_section_id';
public function __construct()
{
if( is_admin() )
{
add_action('admin_menu', array($this, 'add_plugin_page'));
add_action('admin_init', array($this, 'page_init'));
}
}
public function add_plugin_page()
{
// This page will be under "Settings"
add_users_page($this->_page_title, $this->_menu_title, $this->_capability,
$this->_menu_slug, array($this, 'create_admin_page'));
}
public function create_admin_page()
{ ?>
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php echo $this->_page_title; ?></h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields($this->_option_group);
do_settings_sections($this->_menu_slug); ?>
<?php submit_button(); ?>
</form>
</div><?php
}
public function page_init()
{
register_setting($this->_option_group, $this->_option_name, array($this, 'validate_settings'));
// Adds a "group" of settings on the settings page
add_settings_section(
$this->_section_id,
'Setting',
array($this, 'print_section_info'),
$this->_menu_slug
);
// Adds a field to the "group" created with add_settings_section
add_settings_field(
'some_id',
'Some ID(Title)',
array($this, 'create_an_id_field'),
$this->_menu_slug,
$this->_section_id
);
}
public function validate_settings($input)
{
if(is_numeric($input['some_id']))
{
$val = $input['some_id'];
//if(get_option('test_some_id') === FALSE){
//add_option('test_some_id', $val);
//}else{
//update_option('test_some_id', $val);
//}
}
else
{
$val = '';
}
return $input;
}
public function print_section_info()
{
print 'Enter your numeric value below:';
}
public function create_an_id_field(){
$val = $this->get_option('some_id');
?><input type="text" id="input_whatever_unique_id_I_want" name="<?php echo $this->_option_name; ?>[some_id]" value="<?php echo $val; ?>" /><?php
}
public function get_option( $option, $default=false )
{
$opt = get_option($this->_option_name);
if( isset($opt[$option]) )
return $opt[$option];
return $default;
}
}
$wctest = new wctest();
@amereservant
Copy link
Author

I intentionally left this stripped down as much as possible for ease of understanding and faster development. It's a basic snippet to get you started on making your own theme/plugin options page(s) in WordPress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment