Skip to content

Instantly share code, notes, and snippets.

@clifgriffin
Last active December 20, 2015 08:59
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 clifgriffin/6104977 to your computer and use it in GitHub Desktop.
Save clifgriffin/6104977 to your computer and use it in GitHub Desktop.
<?php
class ExamplePlugin {
var $settings = array();
var $prefix = "example_";
public function __construct() {
$this->settings = $this->get_settings_obj( $this->prefix );
add_action('admin_init', array($this, 'save_settings') );
}
// The Settings Framework
function get_settings_obj () {
return get_option("{$this->prefix}settings", false);
}
function set_settings_obj ( $newobj ) {
return update_option("{$this->prefix}settings", $newobj);
}
function update_setting ( $option = false, $newvalue ) {
if( $option === false ) return false;
$this->settings = $this->get_settings_obj($this->prefix);
$this->settings[$option] = $newvalue;
return $this->set_settings_obj($this->settings);
}
function get_setting ( $option = false ) {
if($option === false || ! isset($this->settings[$option]) ) return false;
return apply_filters($thix->prefix . 'get_setting', $this->settings[$option], $option);
}
function add_setting ( $option = false, $newvalue ) {
if($option === false ) return false;
if ( ! isset($this->settings[$option]) ) {
return $this->update_setting($option, $newvalue);
} else return false;
}
function get_field_name($setting, $type = 'string') {
return "{$this->prefix}setting[$setting][$type]";
}
function save_settings()
{
if( isset($_REQUEST["{$this->prefix}setting"]) && check_admin_referer('save_sa_settings','save_the_sa') ) {
$new_settings = $_REQUEST["{$this->prefix}setting"];
foreach( $new_settings as $setting_name => $setting_value ) {
foreach( $setting_value as $type => $value ) {
if( $type == "array" ) {
$this->update_setting($setting_name, (array)explode(";", $value) );
} else {
$this->update_setting($setting_name, $value);
}
}
}
add_action('admin_notices', array($this, 'saved_admin_notice') );
}
}
// An Admin Page Example
function admin_page() {
?>
<input type="textbox" name="<?php echo $this->get_field_name('email_address'); ?>" value="<?php echo $this->get_setting('email_address'); ?>">
<input type="submit" value="Save" />
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment