Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- ## To add a new tab:
- register a new setting using register_setting and create a validation function for the setting below
- Add a new add_settings_section with $id ( first param ) corresponding to the regsister_setting $option_group ( first parm)
- add a new key value set to the $tabs array in mytheme_admin_tabs()
- add blank callback functions for the add_settings_section - this must exist but will not actually display
- ... to add intro text etc add a mew section to tab_intro_text() (sections within tabs seemingly not possible)
- ## To add a new field:
- Add new add_settings_field and add it's callback function
- */
- if ( is_admin() ) :
- include 'theme-options-functions.php';
- include 'theme-options-callbacks.php';
- // Create the tabs and fields
- function do_settings() {
- // -- general
- register_setting( 'general', 'general', 'general_validate' );
- add_settings_section('general', 'General', 'general_function', 'theme_options');
- add_settings_field('general1', 'General 1', 'general1', 'theme_options', 'general');
- add_settings_field('general2', 'General 2', 'general2', 'theme_options', 'general');
- // -- advanced
- register_setting( 'advanced', 'advanced', 'advanced_validate' );
- add_settings_section('advanced', 'Advanced', 'advanced_function', 'theme_options');
- add_settings_field('advanced1', 'Advanced 1', 'advanced1', 'theme_options', 'advanced');
- }
- // Create the options page
- function theme_options() {
- add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'theme_options_page' );
- }
- // Admin Css
- function my_admin_head() {
- $url = get_bloginfo('stylesheet_directory') . '/adminstyle.css'; /* this would normally be 'template_directory' */
- echo "<link rel='stylesheet' href='$url' />\n";
- }
- // Actions
- add_action( 'admin_menu', 'theme_options' );
- add_action( 'admin_init', 'do_settings' );
- add_action('admin_head', 'my_admin_head');
- // Validation Functions
- // -- general
- function general_validate( $input ) {
- return $input;
- }
- // -- advanced
- function advanced_validate( $input ) {
- return $input;
- }
- // Callback functions
- // -- general tab add_settings_field functions
- function general1(){
- $options = get_option('general');
- echo "<p><input size='100' name='general[general1]' type='text' value='{$options['general1']}' /></p>";
- }
- function general2(){
- $options = get_option('general');
- echo "<p><input size='100' name='general[general2]' type='text' value='{$options['general2']}' /></p>";
- }
- // -- advanced tab add_settings_field functions
- function advanced1(){
- $options = get_option('advanced');
- echo "<p><input size='100' name='advanced[advanced1]' type='text' value='{$options['advanced1']}' /></p>";
- }
- // Blank settings_section functions - these must exist but cant be used anywhere under the current design
- function general_function(){
- }
- function advanced_function(){
- }
- // Generate options page
- function theme_options_page() {
- global $pagenow;
- if ( $pagenow == 'themes.php' && $_GET['page'] == 'theme_options' ) :
- if ( isset ( $_GET['tab'] ) ) :
- $tab = $_GET['tab'];
- else:
- $tab = 'general';
- endif;
- mytheme_admin_tabs($tab);
- options_tab_content($tab);
- endif;
- }
- // Generate tabs
- function mytheme_admin_tabs( $current = 'general' ) {
- $tabs = array( 'general' => 'General', 'advanced' => 'Advanced');
- $links = array();
- foreach( $tabs as $tab => $name ) :
- if ( $tab == $current ) :
- $links[] = "<a class='nav-tab nav-tab-active' href='?page=theme_options&tab=$tab'>$name</a>";
- else :
- $links[] = "<a class='nav-tab' href='?page=theme_options&tab=$tab'>$name</a>";
- endif;
- endforeach;
- echo '<h2>';
- foreach ( $links as $link )
- echo $link;
- echo '</h2>';
- }
- // Generate tab content
- function options_tab_content($setting) {
- if ( ! isset( $_REQUEST['settings-updated'] ) )
- $_REQUEST['settings-updated'] = false; ?>
- <div class="wrap">
- <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options' ) . "</h2>"; ?>
- <?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
- <div class="updated fade"><p><strong><?php _e( 'Options saved', 'sampletheme' ); ?></strong></p></div>
- <?php endif; ?>
- <form method="post" action="options.php">
- <?php settings_fields($setting); ?>
- <?php tab_intro_text($setting); ?>
- <?php do_settings_fields( 'theme_options', $setting ) ?>
- <p class="submit"><input type="submit" class="button-primary" value="Save Options" /></p>
- </form>
- </div>
- <?php
- }
- // Generate intro/description text for tabs
- function tab_intro_text($tab){
- echo '<div class = "tab_intro_text">';
- switch ( $tab ) :
- case 'general' :
- echo "This is the general tab";
- break;
- case 'advanced' :
- echo "This is the advanced tab";
- break;
- endswitch;
- echo '</div>';
- }
- endif;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement