Advertisement
Guest User

Wordpress Tabbed Theme Options

a guest
Mar 6th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.28 KB | None | 0 0
  1. <?php
  2. /*
  3. ## To add a new tab:
  4.       register a new setting using register_setting and create a validation function for the setting below
  5.       Add a new add_settings_section with $id ( first param ) corresponding to the regsister_setting $option_group ( first parm)      
  6.       add a new key value set to the $tabs array in mytheme_admin_tabs()
  7.       add blank callback functions for the add_settings_section - this must exist but will not actually display
  8.       ... to add intro text etc add a mew section to tab_intro_text() (sections within tabs seemingly not possible)      
  9. ## To add a new field:
  10.       Add new add_settings_field and add it's callback function
  11. */                              
  12.  
  13. if ( is_admin() ) :  
  14.   include 'theme-options-functions.php';
  15.   include 'theme-options-callbacks.php';
  16.  
  17.   // Create the tabs and fields  
  18.   function do_settings() {
  19.     // -- general
  20.     register_setting( 'general', 'general', 'general_validate' );
  21.     add_settings_section('general', 'General', 'general_function', 'theme_options');  
  22.     add_settings_field('general1', 'General 1', 'general1', 'theme_options', 'general');
  23.     add_settings_field('general2', 'General 2', 'general2', 'theme_options', 'general');  
  24.     // -- advanced
  25.     register_setting( 'advanced', 'advanced', 'advanced_validate' );
  26.     add_settings_section('advanced', 'Advanced', 'advanced_function', 'theme_options');  
  27.     add_settings_field('advanced1', 'Advanced 1', 'advanced1', 'theme_options', 'advanced');
  28.   }  
  29.  
  30.   // Create the options page
  31.   function theme_options() {
  32.     add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'theme_options_page' );
  33.   }  
  34.  
  35.   // Admin Css  
  36.     function my_admin_head() {
  37.     $url = get_bloginfo('stylesheet_directory') . '/adminstyle.css';  /* this would normally be 'template_directory' */
  38.     echo "<link rel='stylesheet' href='$url' />\n";
  39.   }
  40.  
  41.   // Actions
  42.   add_action( 'admin_menu', 'theme_options' );
  43.   add_action( 'admin_init', 'do_settings' );
  44.   add_action('admin_head', 'my_admin_head');
  45.  
  46.  
  47.   // Validation Functions
  48.   // -- general  
  49.   function general_validate( $input ) {
  50.  
  51.     return $input;
  52.   }  
  53.  
  54.   // -- advanced  
  55.   function advanced_validate( $input ) {
  56.    
  57.     return $input;
  58.   }    
  59.  
  60.  
  61.  
  62.   // Callback functions
  63.   // -- general tab add_settings_field functions
  64.   function general1(){
  65.    $options = get_option('general');
  66.    echo "<p><input size='100' name='general[general1]' type='text' value='{$options['general1']}' /></p>";
  67.   }
  68.   function general2(){
  69.    $options = get_option('general');
  70.    echo "<p><input size='100' name='general[general2]' type='text' value='{$options['general2']}' /></p>";
  71.   }
  72.  
  73.  
  74.  
  75.   // -- advanced tab add_settings_field functions
  76.   function advanced1(){
  77.    $options = get_option('advanced');  
  78.    echo "<p><input size='100' name='advanced[advanced1]' type='text' value='{$options['advanced1']}' /></p>";
  79.   }
  80.  
  81.  
  82.   // Blank settings_section functions - these must exist but cant be used anywhere under the current design
  83.   function general_function(){
  84.   }
  85.   function advanced_function(){
  86.   }  
  87.  
  88.  
  89.   // Generate options page
  90.   function theme_options_page() {
  91.     global $pagenow;    
  92.     if ( $pagenow == 'themes.php' && $_GET['page'] == 'theme_options' ) :
  93.       if ( isset ( $_GET['tab'] ) ) :
  94.           $tab = $_GET['tab'];
  95.       else:
  96.           $tab = 'general';
  97.       endif;    
  98.     mytheme_admin_tabs($tab);
  99.     options_tab_content($tab);
  100.     endif;
  101.   }
  102.    
  103.   // Generate tabs
  104.   function mytheme_admin_tabs( $current = 'general' ) {
  105.     $tabs = array( 'general' => 'General', 'advanced' => 'Advanced');
  106.     $links = array();
  107.     foreach( $tabs as $tab => $name ) :
  108.       if ( $tab == $current ) :
  109.           $links[] = "<a class='nav-tab nav-tab-active' href='?page=theme_options&tab=$tab'>$name</a>";
  110.       else :
  111.           $links[] = "<a class='nav-tab' href='?page=theme_options&tab=$tab'>$name</a>";
  112.       endif;
  113.     endforeach;
  114.     echo '<h2>';
  115.     foreach ( $links as $link )
  116.       echo $link;
  117.     echo '</h2>';
  118.   }
  119.  
  120.   // Generate tab content
  121.   function options_tab_content($setting) {
  122.     if ( ! isset( $_REQUEST['settings-updated'] ) )
  123.       $_REQUEST['settings-updated'] = false; ?>    
  124.     <div class="wrap">  
  125.     <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options' ) . "</h2>"; ?>  
  126.     <?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
  127.     <div class="updated fade"><p><strong><?php _e( 'Options saved', 'sampletheme' ); ?></strong></p></div>
  128.     <?php endif; ?>
  129.       <form method="post" action="options.php">    
  130.         <?php settings_fields($setting); ?>
  131.         <?php tab_intro_text($setting); ?>        
  132.         <?php do_settings_fields( 'theme_options', $setting ) ?>
  133.         <p class="submit"><input type="submit" class="button-primary" value="Save Options" /></p>
  134.       </form>  
  135.     </div>  
  136.     <?php
  137.   }
  138.  
  139.   // Generate intro/description text for tabs  
  140.   function tab_intro_text($tab){
  141.     echo '<div class = "tab_intro_text">';
  142.     switch ( $tab ) :
  143.     case 'general' :
  144.     echo "This is the general tab";
  145.     break;
  146.     case 'advanced' :
  147.     echo "This is the advanced tab";
  148.     break;
  149.     endswitch;  
  150.     echo '</div>';
  151.   }
  152.  
  153.  
  154. endif;
  155. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement