'This is the label', 'name' => 'dropdown_name', 'type' => 'select', 'options' => array( 'Option name'=>'option_value', 'Option name 2'=>'option_value_2', 'etc'=>'etc' ) ));?> * To get and use all stored options, just use something like: 'option_name' ?> */ //We include the class we extend from include_once('formelements.php'); class Sircontheme_Options extends Sircontheme_Formelements { protected $fields = array(), $vals = array(), $is_options_page, $has_been_enabled = false, $image_script_included = false; public function add_options_page_in_hook(){ add_theme_page(__('Theme Options', 'sircon_evo'), __('Theme Options', 'sircon_evo'), 'manage_options', 'sircon-theme-options', array($this, 'options_page_output')); if($_GET['page'] === 'sircon-theme-options'){ add_action( 'admin_enqueue_scripts', array($this, 'include_colorpicker')); } } /* needs to be run to hook into everything we need for metabox handling */ private function enable_options_page(){ if($this->has_been_enabled) return; /* Already done, move along */ add_action('admin_menu', array($this, 'add_options_page_in_hook')); add_action('admin_init', array($this, 'save_options'),1); $this->has_been_enabled = true; } /* Simply allow wordpress to output the options page title, and save some options */ public function options_page_output(){ //Include colorpicker //begin output //Default styles! echo ''; //prepare some vars and make sure changes from last click is saved $this->is_options_page = true; $this->vals = $this->get_options(); $hash_value = isset($_GET['hash']) ? $_GET['hash'] : 'primary'; $current_primary = !isset($_GET['hash']) || $_GET['hash'] === 'primary' ? ' current-fieldset' : ''; echo '

'.__('Theme Options', 'sircon_evo').'

'; echo '
'.__('Main', 'sircon_evo').'
'; foreach($this->fields as $field){ if(isset($field['reset'])){ $this->reset_button($field['reset']); continue; } //Get saved value, if any $saved_value = $this->get_option($field['name']); if($saved_value === false){$saved_value = '';} //Get enabled state, if any $enabled = $this->get_option($field['name'].'-enabled'); if($enabled === '' || $enabled === false){ $enabled = false; }else{$enabled = true;} //Fetch output $field_output = $this->get_field($field, $saved_value, $enabled); if($field['type'] != 'pagebreak'){ echo '
'.$field_output.'
'; }else{ $this->save_button(); echo $field_output; } } $this->save_button(); echo '
'; echo '
'; //Some script for the tabs ?>'; } public function save_button(){ echo ''; } public function reset_button($label = 'reset'){ echo ''; } //Erease all saved data for all fields public function add_reset_button($label = 'reset'){ $this->add_option(array( 'reset' => $label )); } /* Return an array of all saved options */ public function get_options(){ $ret = array(); foreach ($this->fields as $fieldname => $v){ $ret[$fieldname] = $this->get_option($fieldname); } return $ret; } /* Return an array of all scss options */ public function get_scss_options(){ $ret = array(); foreach ($this->fields as $fieldname => $v){ if(isset($v['scss']) && $v['scss']){ $enabled = !isset($v['enabler']) || ($this->get_option($fieldname.'-enabled') == 'on'); if(!$enabled){continue;} $scss_value = $this->get_option($fieldname); if(!$scss_value){continue;} $ret[$fieldname] = $scss_value; } } return $ret; } public function get_option($fieldname){ $prefixed_fieldname = $this->name_prefix.$fieldname; //Get the existing value from database $existing_value = get_option($prefixed_fieldname); if(gettype($existing_value) == 'string'){ $existing_value = stripslashes($existing_value); } //Did we get a value? if($existing_value === false){ //No value! Try to use default parameter as value $fieldparams = $this->fields[$fieldname]; if(isset($fieldparams['default'])){ $saved_value = $fieldparams['default']; }else{$saved_value = '';} }else{ $saved_value = $existing_value; } //When not in controlpanel, we do the shortcodes! if(!is_admin() && $saved_value){ $saved_value = do_shortcode($saved_value); } return $saved_value; } /** * */ public function add_option($param1 = 'text', $param2 = 'input-name', $param3 = ''){ $this->enable_options_page(); if(is_array($param1)){ $params = $param1; }else{ $params = array( 'type'=> $param1, 'name' => $param2, 'label' => $param3 ); } $this->fields[$params['name']] = $params; } /** * Save all options, but only if saving is supposed to happen. * This function runs automatically when it should, and must remain public for Wordpress to understand how to run it. * Do not use manually. * * @Possible_todo: Rewrite something to make this not-manually-usable function actually become private * @herman hotfix -> added some issets / function was breaking context when wp_debug was on. */ public function save_options(){ if((isset($_REQUEST['page'])) && ($_REQUEST['page'] == 'sircon-theme-options')) wp_enqueue_media(); if((!isset($_REQUEST['save_sircontheme_options'])) || (isset($_REQUEST['save_sircontheme_options']) && !$_REQUEST['save_sircontheme_options'])) return; //Users can sometimes access a "reset everything" button. This is where it is used $is_full_reset = (isset($_POST['sircontheme-options-full-reset-for-reals']) && $_POST['sircontheme-options-full-reset-for-reals'] == 'true'); if($is_full_reset){ //User wants to reset it all! $this->delete_all_saved_values(); }else{ //Normal save $nosavy = array('pagebreak'); if($this->fields){ foreach($this->fields as $fieldname=>$params){ if(in_array($params['type'], $nosavy)){continue;} $fieldname = $this->name_prefix.$fieldname; $ok = update_option($fieldname, $_REQUEST[$fieldname]); //Also remember any enabled-state if(isset($params['enabler'])){ $enabler = $fieldname.'-enabled'; $ok = update_option($enabler, $_REQUEST[$enabler]); } } } } //Remember the tab we are on $hash = $_REQUEST['hash'] ? $_REQUEST['hash'] : 'primary'; //remember when we last saved update_option('sircontheme_last-time-options-saved', time()); header('Location:'.admin_url('/themes.php?page=sircon-theme-options').'&hash='.$hash); die; } //Remove all sircontheme saved values (will not touch other values) public function delete_all_saved_values(){ if(!$this->fields){return;} foreach($this->fields as $fieldname=>$params){ $fieldname = $this->name_prefix.$fieldname; delete_option($fieldname); } } } /* Return the theme options object */ function get_sircontheme_options_object(){ global $Sircontheme_Options; if(!isset($Sircontheme_Options)){ $Sircontheme_Options = new Sircontheme_Options(); } return $Sircontheme_Options; } /** * Retrieve a theme option. */ function get_sircontheme_option($name){ $theme_options_obj = get_sircontheme_options_object(); return $theme_options_obj->get_option($name); } /* Get all options*/ function get_sircontheme_options(){ $theme_options_obj = get_sircontheme_options_object(); return $theme_options_obj->get_options(); } /* Get all scss style fields */ function get_sircontheme_scss_options(){ $theme_options_obj = get_sircontheme_options_object(); return $theme_options_obj->get_scss_options(); } /** * Set a new theme option. * @recentchangebyjohn: Less params in final class-member-call. Renamed class */ function add_sircontheme_option($args = array()) { $arrParams = func_get_args(); $default = array ( 'type' => 'text', 'name' => 'demo' ); $settings = array_merge($default, $args); $theme_options_obj = get_sircontheme_options_object(); $theme_options_obj->add_option($settings); } function add_sircontheme_scss_variable($type, $args = array()) { $args['scss'] = true; add_sircontheme_option_type($type, $args); } function add_sircontheme_option_type($type, $args = array()) { $args['type'] = $type; add_sircontheme_option($args); } function add_sircontheme_option_reset_button($label = 'Reset'){ $theme_options_obj = get_sircontheme_options_object(); $theme_options_obj->add_reset_button($label); }