'default_field', // the ID of the setting in our options array, and the ID of the HTML form element 'title' => 'Default Field', // the label for the HTML form element 'desc' => 'This is a default description.', // the description displayed under the HTML form element 'std' => '', // the default value for this setting 'type' => 'text', // the HTML form element to use 'section' => 'main_section', // the section this setting belongs to � must match the array key of a section in blogBox_options_page_sections() 'choices' => array(), // (optional): the values in radio buttons or a drop-down menu 'class' => '' // the HTML form element class. Is used for validation purposes and may be also use for styling if needed. ); // "extract" to be able to use the array keys as variables in our function output below extract( wp_parse_args( $args, $defaults ) ); // additional arguments for use in form field output in the function blogBox_form_field_fn! $field_args = array( 'type' => $type, 'id' => $id, 'desc' => $desc, 'std' => $std, 'choices' => $choices, 'label_for' => $id, 'class' => $class ); add_settings_field( $id, $title, 'blogBox_form_field_fn', __FILE__, $section, $field_args ); } function blogBox_register_settings(){ /** * Register our setting, settings sections and settings fields */ // get the settings sections array $settings_output = blogBox_get_settings(); $blogBox_option_name = $settings_output['blogBox_option_name']; //setting register_setting($blogBox_option_name, $blogBox_option_name, 'blogBox_validate_options' ); //sections if(!empty($settings_output['blogBox_page_sections'])){ // call the "add_settings_section" for each! foreach ( $settings_output['blogBox_page_sections'] as $id => $title ) { add_settings_section( $id, $title, 'blogBox_section_fn', __FILE__); } } //fields if(!empty($settings_output['blogBox_page_fields'])){ // call the "add_settings_field" for each! foreach ($settings_output['blogBox_page_fields'] as $option) { blogBox_create_settings_field($option); } } } add_action( 'admin_init', 'blogBox_register_settings' ); function blogBox_settings_scripts(){ /** * Group scripts (js & css) */ wp_enqueue_style('blogBox_theme_settings_css', get_template_directory_uri() . '/library/blogBox_options_css.css'); wp_enqueue_script( 'blogBox_theme_settings_js', get_template_directory_uri() . '/library/blogBox_options_js.js', array('jquery'), '1', true ); wp_enqueue_style('farbtastic'); wp_enqueue_script('farbtastic'); } add_action('admin_init', 'blogBox_settings_scripts'); function blogBox_add_menu(){ /** * The admin menu pages */ global $blogBox_settings_page; //$settings_output = blogBox_get_settings(); // As a "top level" menu $blogBox_settings_page = add_theme_page('blogBox Settings','blogBox Settings','edit_theme_options', BLOGBOX_PAGE_BASENAME, 'blogBox_settings_page_fn'); } add_action( 'admin_menu', 'blogBox_add_menu' ); function blogBox_get_the_tab() { /** * Helper function: Check for tabs and return the current tab name * * @return string */ global $pagenow; // set default tab $default_tab = 'general'; // read the current tab when on our settings page $current_tab = (isset($_GET['tab']) ? $_GET['tab'] : $default_tab); //use a different way to read the tab when the form submits if ($pagenow == 'options.php') { // need to read the tab name so we explode()! $parts = explode('&tab=', $_POST['_wp_http_referer']); // http://codex.wordpress.org/Function_Reference/wp_referer_field // count the "exploded" parts $partsNum = count($parts); // account for "&settings-updated=true" (we do not want that to be part of our return value!) // is it "&settings-updated=true" there? - check for the "&" $settings_updated = strpos($parts[1],"&"); // filter it out and get the tab name $tab_name = ($settings_updated !== FALSE ? substr($parts[1],0,$settings_updated) : $parts[1]); // use if found, otherwise pass the default tab name $current_tab = ($partsNum == 2 ? trim($tab_name) : $default_tab); } return $current_tab; } function blogBox_settings_page_header() { /** * Helper function: Creates settings page title and tabs (if needed) * * @return echos output */ //This function gets the header title and tabs and prints them out // get the tabs $settings_output = blogBox_get_settings(); $tabs = $settings_output['blogBox_page_tabs']; // get the current tab $current_tab = blogBox_get_the_tab(); // display the icon and page title echo '

'; echo '

' . $settings_output['blogBox_page_title'] . '

'; // check for tabs if ($tabs !='') { // wrap each in anchor html tags $links = array(); foreach( $tabs as $tab => $name ) { // set anchor class $class = ($tab == $current_tab ? 'nav-tab nav-tab-active' : 'nav-tab'); $page = $_GET['page']; // the link $links[] = "$name"; } echo ''; } } /* ------------------------------------------------------------------------------------------------ * * Callback functions * ------------------------------------------------------------------------------------------------ */ function blogBox_section_fn($desc) { /* * Section HTML, displayed before the first option * @return echoes output */ //echo '

Settings for this section

'; } function blogBox_form_field_fn($args = array()) { /** * Form Fields HTML * All form field types share the same function!! * @return echoes output */ extract( $args ); // get the settings sections array $settings_output = blogBox_get_settings(); $blogBox_option_name = $settings_output['blogBox_option_name']; //echo $bloBox_option_name; $options = get_option($blogBox_option_name); // pass the standard value if the option is not yet set in the database if (!isset($options[$id]) || $options[$id] == "" && $type !== 'checkbox') { $options[$id] = $std; } // additional field class. output only if the class is defined in the create_setting arguments $field_class = ($class != '') ? ' ' . $class : ''; // switch html display based on the setting type. switch ( $type ) { case 'text': $options[$id] = stripslashes($options[$id]); $options[$id] = esc_attr( $options[$id]); //echo ''; echo ""; echo ($desc != '') ? "
$desc" : ""; break; case "multi-text": foreach($choices as $item) { $item = explode("|",$item); // cat_name|cat_slug $item[0] = esc_html($item[0]); if (!empty($options[$id])) { foreach ($options[$id] as $option_key => $option_val){ if ($item[1] == $option_key) { $value = $option_val; } } } else { $value = ''; } echo "$item[0]:
"; } echo ($desc != '') ? "$desc" : ""; break; case 'textarea': $options[$id] = stripslashes($options[$id]); $options[$id] = esc_html( $options[$id]); echo ""; echo ($desc != '') ? "
$desc" : ""; break; case 'select': echo ""; echo ($desc != '') ? "
$desc" : ""; break; case 'select2': echo ""; echo ($desc != '') ? "
$desc" : ""; break; case 'checkbox': echo ""; echo ($desc != '') ? "
$desc" : ""; break; case "multi-checkbox": foreach($choices as $item) { $item = explode("|",$item); $item[0] = esc_html($item[0]); $checked = ''; if ( isset($options[$id][$item[1]]) ) { if ( $options[$id][$item[1]] == 'true') { $checked = 'checked="checked"'; } } echo " $item[0]
"; } echo ($desc != '') ? "
$desc" : ""; break; } } function blogBox_settings_page_fn() { /* * Admin Settings Page HTML * * @return echoes output */ // get the settings sections array $settings_output = blogBox_get_settings(); $tab = blogBox_get_the_tab(); ?>
'; // http://codex.wordpress.org/Function_Reference/do_settings_sections do_settings_sections(__FILE__); ?>

array('href' => array (),'title' => array ()), 'b' => array(), 'em' => array (), 'i' => array (), 'strong' => array() ); $input[$option['id']] = trim($input[$option['id']]); // trim whitespace $input[$option['id']] = force_balance_tags($input[$option['id']]); // find incorrectly nested or missing closing tags and fix markup $input[$option['id']] = wp_kses( $input[$option['id']], $allowed_html); // need to add slashes still before sending to the database $valid_input[$option['id']] = addslashes($input[$option['id']]); break; } break; case "multi-text": // this will hold the text values as an array of 'key' => 'value' unset($textarray); $text_values = array(); foreach ($option['choices'] as $k => $v ) { // explode the connective $pieces = explode("|", $v); $text_values[] = $pieces[1]; } foreach ($text_values as $v ) { // Check that the option isn't empty if (!empty($input[$option['id'] . '|' . $v])) { // If it's not null, make sure it's sanitized, add it to an array switch ($option['class']) { // different sanitation actions based on the class create you own cases as you need them //for numeric input case 'numeric': //accept the input only if is numberic! $input[$option['id'] . '|' . $v]= trim($input[$option['id'] . '|' . $v]); // trim whitespace $input[$option['id'] . '|' . $v]= (is_numeric($input[$option['id'] . '|' . $v])) ? $input[$option['id'] . '|' . $v] : ''; break; // a "cover-all" fall-back when the class argument is not set default: // strip all html tags and white-space. $input[$option['id'] . '|' . $v]= sanitize_text_field($input[$option['id'] . '|' . $v]); // need to add slashes still before sending to the database $input[$option['id'] . '|' . $v]= addslashes($input[$option['id'] . '|' . $v]); break; } // pass the sanitized user input to our $textarray array $textarray[$v] = $input[$option['id'] . '|' . $v]; } else { $textarray[$v] = ''; } } // pass the non-empty $textarray to our $valid_input array if (!empty($textarray)) { $valid_input[$option['id']] = $textarray; } break; case 'textarea': //switch validation based on the class! switch ( $option['class'] ) { //for only inline html case 'inlinehtml': // accept only inline html $input[$option['id']] = trim($input[$option['id']]); // trim whitespace $input[$option['id']] = force_balance_tags($input[$option['id']]); // find incorrectly nested or missing closing tags and fix markup $input[$option['id']] = addslashes($input[$option['id']]); //wp_filter_kses expects content to be escaped! $valid_input[$option['id']] = wp_filter_kses($input[$option['id']]); //calls stripslashes then addslashes break; //for no html case 'nohtml': //accept the input only after stripping out all html, extra white space etc! $input[$option['id']] = sanitize_text_field($input[$option['id']]); // need to add slashes still before sending to the database $valid_input[$option['id']] = addslashes($input[$option['id']]); break; //for allowlinebreaks case 'allowlinebreaks': //accept the input only after stripping out all html, extra white space etc! $input[$option['id']] = wp_strip_all_tags($input[$option['id']]); // need to add slashes still before sending to the database $valid_input[$option['id']] = addslashes($input[$option['id']]); break; // a "cover-all" fall-back when the class argument is not set default: // accept only limited html //my allowed html $allowed_html = array( 'a' => array('href' => array (),'title' => array ()), 'b' => array(), 'blockquote' => array('cite' => array ()), 'br' => array(), 'dd' => array(), 'dl' => array(), 'dt' => array(), 'em' => array (), 'i' => array (), 'li' => array(), 'ol' => array(), 'p' => array(), 'q' => array('cite' => array ()), 'strong' => array(), 'ul' => array('class'=>array()), 'h1' => array('align' => array (),'class' => array (),'id' => array (), 'style' => array ()), 'h2' => array('align' => array (),'class' => array (),'id' => array (), 'style' => array ()), 'h3' => array('align' => array (),'class' => array (),'id' => array (), 'style' => array ()), 'h4' => array('align' => array (),'class' => array (),'id' => array (), 'style' => array ()), 'h5' => array('align' => array (),'class' => array (),'id' => array (), 'style' => array ()), 'h6' => array('align' => array (),'class' => array (),'id' => array (), 'style' => array ()) ); $input[$option['id']] = trim($input[$option['id']]); // trim whitespace $input[$option['id']] = force_balance_tags($input[$option['id']]); // find incorrectly nested or missing closing tags and fix markup $input[$option['id']] = wp_kses( $input[$option['id']], $allowed_html); // need to add slashes still before sending to the database $valid_input[$option['id']] = addslashes($input[$option['id']]); break; } break; case 'select': // check to see if the selected value is in our approved array of values! $valid_input[$option['id']] = (in_array( $input[$option['id']], $option['choices']) ? $input[$option['id']] : '' ); break; case 'select2': // process $select_values $select_values = array(); foreach ($option['choices'] as $k => $v) { // explode the connective $pieces = explode("|", $v); $select_values[] = $pieces[1]; } // check to see if selected value is in our approved array of values! $valid_input[$option['id']] = (in_array( $input[$option['id']], $select_values) ? $input[$option['id']] : '' ); break; case 'checkbox': // if it's not set, default to null! if (!isset($input[$option['id']])) { $input[$option['id']] = null; } // Our checkbox value is either 0 or 1 $valid_input[$option['id']] = ( $input[$option['id']] == 1 ? 1 : 0 ); break; case 'multi-checkbox': unset($checkboxarray); $check_values = array(); foreach ($option['choices'] as $k => $v ) { // explode the connective $pieces = explode("|", $v); $check_values[] = $pieces[1]; } foreach ($check_values as $v ) { // Check that the option isn't null if (!empty($input[$option['id'] . '|' . $v])) { // If it's not null, make sure it's true, add it to an array $checkboxarray[$v] = 'true'; } else { $checkboxarray[$v] = 'false'; } } // Take all the items that were checked, and set them as the main option if (!empty($checkboxarray)) { $valid_input[$option['id']] = $checkboxarray; } break; } } return $valid_input; // return validated input } function blogBox_show_msg($message, $msgclass = 'info') { /** * Helper function for creating admin messages * src: http://www.wprecipes.com/how-to-show-an-urgent-message-in-the-wordpress-admin-area * * @param (string) $message The message to echo * @param (string) $msgclass The message class * @return echoes the message */ echo "
$message
"; } function blogBox_admin_msgs() { /** * Callback function for displaying admin messages * * @return calls blogBox_show_msg() */ // check for our settings page - need this in conditional further down if(isset($_GET['page'])){ $blogBox_settings_pg = strpos($_GET['page'], BLOGBOX_PAGE_BASENAME); } else { $blogBox_settings_pg = FALSE; } // collect setting errors/notices: //http://codex.wordpress.org/Function_Reference/get_settings_errors $set_errors = get_settings_errors(); //display admin message only for the admin to see, only on our settings page and only when setting errors/notices are returned! if(current_user_can ('manage_options') && $blogBox_settings_pg !== FALSE && !empty($set_errors)){ // have our settings succesfully been updated? if($set_errors[0]['code'] == 'settings_updated' && isset($_GET['settings-updated'])){ blogBox_show_msg("

" . $set_errors[0]['message'] . "

", 'updated'); // have errors been found? }else{ // there maybe more than one so run a foreach loop. foreach($set_errors as $set_error){ // set the title attribute to match the error "setting title" - need this in js file blogBox_show_msg("

" . $set_error['message'] . "

", 'error'); } } } } add_action('admin_notices', 'blogBox_admin_msgs'); ?>