Advertisement
amereservant

WordPress - Custom Theme Settings & Options

Sep 3rd, 2011
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.11 KB | None | 0 0
  1. <?php
  2. /* --- No need to wrap these with is_admin() since they are only ran in the admin section -- */
  3. // Insert the theme page menu item
  4. add_action('admin_menu', 'option_page_menu_insert');
  5.  
  6. // Initiate theme options and page
  7. add_action('admin_init', 'register_theme_settings');
  8.  
  9. add_action('init', 'add_my_options');
  10.  
  11. /**
  12.  * Add a menu item inter the 'Appearance' menu
  13.  *
  14.  * Function Definition:
  15.  * add_options_page(unique text id,  displayed title, function to display, callback page)
  16.  */
  17. function option_page_menu_insert()
  18. {
  19.     add_theme_page( 'WSU Theme Options', 'WSU Theme Options', 'edit_theme_options', 'WSU_Theme_Options', 'theme_options_page');
  20. }
  21.  
  22. /**
  23.  * Add Theme Options
  24.  *
  25.  * This adds the theme's options if they don't already exist or if they aren't in an array.
  26.  */
  27. function add_my_options()
  28. {
  29.     // Define option default values here ...
  30.     $defaults = array(
  31.         'wsuy_logo'     => '',
  32.         'wsuy_color'    => ''
  33.     );
  34.     // Try getting our theme options.  If they don't exist, it will return (bool)false
  35.     $options = get_option('wsuy_theme_options');
  36.    
  37.     // If it's an array, we won't proceed.
  38.     if( is_array($options) ) return;
  39.    
  40.     if($options === false)
  41.         add_option('wsuy_theme_options', $defaults);
  42.     else
  43.         // Options weren't an array, so we'll go ahead and replace it with our defaults...
  44.         update_option('wsuy_theme_options', $defaults);
  45.  
  46. }
  47.  
  48. function register_theme_settings()
  49. {
  50.     // Create the main setting group: 'wsuy-theme-options'
  51.     register_setting('wsuy_theme_options', 'wsuy_theme_options', 'validate');
  52.  
  53.    /* Add a settings group sub-section: 'wsuy-theme-options-section'
  54.     * Function Definition:
  55.     * add_settings_section(unique text id,  displayed title, function to display, callback page)
  56.     */
  57.     add_settings_section('wsuy_theme_options_section', 'WSU Theme (Yoko): Options', 'section_description', __FILE__);
  58.  
  59.    /* Define theme options fields:
  60.     * Function Definition:
  61.     * add_settings_field(unique  id, displayed title, display, callback page, section id)
  62.     */
  63.     add_settings_field('wsuy_logo',  'Website logo',         'field_logo',  __FILE__, 'wsuy_theme_options_section');
  64.     add_settings_field('wsuy_color', 'Website header color', 'field_color', __FILE__, 'wsuy_theme_options_section');
  65.  
  66. }
  67.  
  68. function field_logo()
  69. {
  70.     $options = get_option('wsuy_theme_options');
  71.     $path    = get_template_directory_uri() .'logos/';
  72.     ?>
  73.     <input id='wsuy_logo' name='wsuy_theme_options[wsuy_logo]' size='40' type='text' value='<?php echo $options['wsuy_logo']; ?>' style='display:none'/><br>
  74.  
  75.     <ul id='logo'>
  76.         <li id="l1"><img class="logo" src="<?php echo $path; ?>ce.PNG" width="100px;"/></li>
  77.         <li id="l2"><img class="logo" src="<?php echo $path; ?>cepm.png" width="100px;"/></li>
  78.         <li id="l3"><img class="logo" src="<?php echo $path; ?>west.PNG" width="100px;"/></li>
  79.         <li id="l4"><img class="logo" src="<?php echo $path; ?>concurrent.PNG" width="100px;"/></li>
  80.         <li id="l5"><img class="logo" src="<?php echo $path; ?>conferences.PNG" width="100px;"/></li>
  81.         <li id="l6"><img class="logo" src="<?php echo $path; ?>early-credit.PNG" width="100px;"/></li>
  82.         <li id="l7"><img class="logo" src="<?php echo $path; ?>hybrid.PNG" width="100px;"/></li>
  83.         <li id="l8"><img class="logo" src="<?php echo $path; ?>kaysville.PNG" width="100px;"/></li>
  84.         <li id="l9"><img class="logo" src="<?php echo $path; ?>lifelong-learning.PNG" width="100px;"/></li>
  85.         <li id="l10"><img class="logo" src="<?php echo $path; ?>morgan.PNG" width="100px;"/></li>
  86.         <li id="l11"><img class="logo" src="<?php echo $path; ?>police.PNG" width="100px;"/></li>
  87.         <li id="l12"><img class="logo" src="<?php echo $path; ?>prof-dev.PNG" width="100px;"/></li>
  88.         <li id="l13"><img class="logo" src="<?php echo $path; ?>study-abroad.PNG" width="100px;"/></li>
  89.         <div style="clear:both"></div>
  90.     </ul><?php
  91. }
  92.  
  93. function field_color() {
  94.     $options = get_option('wsuy_theme_options');
  95.     echo "<input id='wsuy_color' name='wsuy_theme_options[wsuy_color]' size='40' type='color' data-hex='true' value='{$options['wsuy_color']}' />";
  96. }
  97.  
  98. function section_description() {}
  99.  
  100. function validate($input) {
  101.     // Validate our settings values before saving ....
  102.     return $input;
  103. }
  104.  
  105. function theme_options_page() {
  106.     //Check the users permission level
  107.     if( !current_user_can('manage_options') )
  108.         wp_die( __('You do not have sufficient permissions to access this page.') );
  109.    
  110.     //Generate the html for the options page:
  111.     ?>
  112.     <style type="text/css">
  113.         #logo{ list-style:none; display:block; background:#eaeaea; }
  114.         #logo li{ display:inline; float:left; margin:10px; width:120px; height:30px; border:1px solid #d5d5d5;
  115.                   background:#fff; padding:10px; cursor:pointer; border-radius:5px; -moz-border-radius:5px;
  116.                   -webkit-border-radius:5px; text-align:center; }
  117.         #logo li:hover{ border:1px solid #aeaeae; }
  118.         .selected{ background:#fffce0 !important; border:1px solid #aeaeae !important; }
  119.     </style>
  120.     <div class="wrap">
  121.         <h1>WSU Theme Options</h1>
  122.         <form method="post" action="options.php">
  123.             <?php settings_fields('wsuy_theme_options'); ?>
  124.             <?php do_settings_sections(__FILE__); ?>
  125.             <div class="clear"><br /></div>
  126.             <input style="margin-left:800px;" name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" class="button-primary"/>
  127.         </form>
  128.     </div>
  129.     <!-- ColorPicker Javascript -->
  130.     <script type="text/javascript" src="http://meta100.github.com/mColorPicker/javascripts/mColorPicker_min.js" charset="UTF-8"></script>
  131.     <script type="text/javascript">
  132.         jQuery(document).ready(function($) {
  133.             $("#logo li").click(function() {
  134.                 file = $(this).find("img").attr("src");
  135.                 $("#wsuy_logo").val(file);
  136.                 $("#logo li").removeClass("selected");
  137.                 $(this).addClass("selected");
  138.             });
  139.         });        
  140.     </script><?php
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement