Advertisement
Guest User

theme option

a guest
May 31st, 2011
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. <?php
  2.  
  3. $settings = get_current_theme().'-options'; // do not change!
  4.  
  5. $defaults = array( // define our defaults
  6. 'slide_1_title' => '',
  7. 'slide_1_desc' =>'',
  8. 'slide_1_link' => '',
  9. 'slide_2_title' => '',
  10. 'slide_2_desc' =>'',
  11. 'slide_2_link' => '',
  12. 'slide_3_title' => '',
  13. 'slide_3_desc' =>'',
  14. 'slide_3_link' => '',
  15.  
  16. 'slider_interval' => 6000,
  17. 'tracking' => '<!--tracking code goes here-->' // <-- no comma after the last option
  18. );
  19.  
  20. // push the defaults to the options database,
  21. // if options don't yet exist there.
  22. add_option($settings, $defaults, '', 'yes');
  23.  
  24. // this function registers our settings in the db
  25. add_action('admin_init', 'register_theme_settings');
  26. function register_theme_settings() {
  27. global $settings;
  28. register_setting($settings, $settings, 'validate_options_cb');
  29. }
  30. // this function adds the settings page to the menu
  31. add_action('admin_menu', 'add_theme_options_menu');
  32. function add_theme_options_menu() {
  33. add_menu_page("Options", "Options", 'edit_themes', 'custom-option-main-settings', 'theme_settings_admin', get_template_directory_uri().'/favicon.png');
  34. }
  35.  
  36. function theme_settings_admin() { ?>
  37. <?php theme_options_css_js(); ?>
  38. <div class="wrap">
  39. <?php
  40. // display the proper notification if Saved/Reset
  41. global $settings, $defaults;
  42.  
  43. // display icon next to page title
  44. screen_icon('options-general');
  45. ?>
  46. <h2><?php echo get_current_theme() . ' '; _e('Theme Options'); ?></h2>
  47. <form method="post" action="options.php">
  48. <?php settings_fields($settings); // important! ?>
  49.  
  50. <!--left column-->
  51. <div class="metabox-holder mbleft">
  52.  
  53. <div class="postbox">
  54. <h3><?php _e("Homepage Slider", 'custom-option'); ?></h3>
  55. <div class="inside">
  56. <p><strong>Slide 1:</strong></p>
  57. <p>Title:<br />
  58. <input type="text" name="<?php echo $settings; ?>[slide_1_title]" value="<?php echo custom_option('slide_1_title'); ?>" size="60" /></p>
  59. <p>description:<br />
  60. <textarea name="<?php echo $settings; ?>[slide_1_desc]" value="<?php echo custom_option('slide_1_desc'); ?>" cols=60 rows=5></textarea></p>
  61. <p>link:<br />
  62. <input type="text" name="<?php echo $settings; ?>[slide_1_link]" value="<?php echo custom_option('slide_1_link'); ?>" size="60" /></p>
  63.  
  64. <p><strong>Slide 2:</strong></p>
  65. <p>Title:<br />
  66. <input type="text" name="<?php echo $settings; ?>[slide_2_title]" value="<?php echo custom_option('slide_2_title'); ?>" size="60" /></p>
  67. <p>description:<br />
  68. <textarea name="<?php echo $settings; ?>[slide_2_desc]" value="<?php echo custom_option('slide_2_desc'); ?>" cols=60 rows=5></textarea></p>
  69. <p>link:<br />
  70. <input type="text" name="<?php echo $settings; ?>[slide_2_link]" value="<?php echo custom_option('slide_2_link'); ?>" size="60" /></p>
  71.  
  72. <p><strong>Slide 3:</strong></p>
  73. <p>Title:<br />
  74. <input type="text" name="<?php echo $settings; ?>[slide_3_title]" value="<?php echo custom_option('slide_3_title'); ?>" size="60" /></p>
  75. <p>description:<br />
  76. <textarea name="<?php echo $settings; ?>[slide_3_desc]" value="<?php echo custom_option('slide_3_desc'); ?>" cols=60 rows=5></textarea></p>
  77. <p>link:<br />
  78. <input type="text" name="<?php echo $settings; ?>[slide_3_link]" value="<?php echo custom_option('slide_3_link'); ?>" size="60" /></p>
  79.  
  80. <p><?php _e("Select the interval for slide transistions (in seconds)", 'custom-option'); ?><br />
  81. <select name="<?php echo $settings; ?>[slider_interval]">
  82. <option style="padding-right:10px;" value="1000" <?php selected('1000', custom_option('slider_interval')); ?>><?php _e("1", 'custom-option'); ?></option>
  83. <option style="padding-right:10px;" value="2000" <?php selected('2000', custom_option('slider_interval')); ?>><?php _e("2", 'custom-option'); ?></option>
  84. <option style="padding-right:10px;" value="3000" <?php selected('3000', custom_option('slider_interval')); ?>><?php _e("3", 'custom-option'); ?></option>
  85. <option style="padding-right:10px;" value="4000" <?php selected('4000', custom_option('slider_interval')); ?>><?php _e("4", 'custom-option'); ?></option>
  86. <option style="padding-right:10px;" value="5000" <?php selected('5000', custom_option('slider_interval')); ?>><?php _e("5", 'custom-option'); ?></option>
  87. <option style="padding-right:10px;" value="6000" <?php selected('6000', custom_option('slider_interval')); ?>><?php _e("6", 'custom-option'); ?></option>
  88. <option style="padding-right:10px;" value="7000" <?php selected('7000', custom_option('slider_interval')); ?>><?php _e("7", 'custom-option'); ?></option>
  89. <option style="padding-right:10px;" value="8000" <?php selected('8000', custom_option('slider_interval')); ?>><?php _e("8", 'custom-option'); ?></option>
  90. <option style="padding-right:10px;" value="9000" <?php selected('9000', custom_option('slider_interval')); ?>><?php _e("9", 'custom-option'); ?></option>
  91. <option style="padding-right:10px;" value="10000" <?php selected('10000', custom_option('slider_interval')); ?>><?php _e("10", 'custom-option'); ?></option>
  92. </select></p>
  93. </div>
  94.  
  95. </div>
  96.  
  97.  
  98. <p class="submit">
  99. <input type="submit" name="save-settings" class="button-primary" value="<?php _e('Save Settings', 'custom-option') ?>" />
  100. <!--<input type="submit" class="button-highlighted" name="<?php echo $settings; ?>[reset]" value="<?php _e('Reset Settings', 'custom-option'); ?>" />-->
  101. </p>
  102. </div>
  103. <!--end first column-->
  104.  
  105. <!--right column-->
  106.  
  107. <div class="metabox-holder mbright">
  108.  
  109.  
  110. <div class="postbox">
  111. <h3><?php _e("Analytics Code", 'custom-option'); ?></h3>
  112. <div class="inside">
  113. <p>If you use a service such as <a href="http://www.google.com/analytics/">Google Analytics</a> to track analytics on your site, paste the code below (it will be inserted into the footer):</p>
  114. <p>
  115. <textarea name="<?php echo $settings; ?>[tracking]" cols=30 rows=5><?php echo stripslashes(custom_option('tracking')); ?></textarea>
  116. </p>
  117. </div>
  118. </div>
  119.  
  120. <div class="postbox">
  121. <h3><?php _e("RSS Email Subscription", 'custom-option'); ?></h3>
  122. <div class="inside">
  123. <p><?php _e("Enter your FeedBurner ID:", 'custom-option'); ?><br />
  124. <input type="text" name="<?php echo $settings; ?>[feedburner]" value="<?php echo custom_option('feedburner'); ?>" size="30" /></p>
  125. </div>
  126. </div>
  127.  
  128.  
  129.  
  130. </div>
  131. <!--end second column-->
  132.  
  133. </form>
  134.  
  135. </div><!--end .wrap-->
  136. <?php }
  137.  
  138. // Pull theme options from database
  139. function custom_option($key) {
  140. global $settings;
  141. $option = get_option($settings);
  142. if(isset($option[$key])) return $option[$key];
  143. else return FALSE;
  144. }
  145.  
  146. // validating the options
  147.  
  148. function validate_options_cb($input){
  149. //do regular validation stuff
  150. //...
  151. //...
  152.  
  153. //get all options
  154. $options = get_option(get_current_theme().'-options');
  155. //update only the neede options
  156. foreach ($input as $key => $value){
  157. $options[$key] = $value;
  158. }
  159. //return all options
  160. return $options;
  161.  
  162. }
  163.  
  164. // add CSS and JS if necessary
  165. function theme_options_css_js() {
  166. ?>
  167.  
  168. <style type="text/css">
  169. .metabox-holder {
  170. float: left;
  171. margin: 0; padding: 0 10px 0 0;
  172. }
  173. .metabox-holder {
  174. float: left;
  175. margin: 0; padding: 0 10px 0 0;
  176. }
  177. .metabox-holder .postbox .inside {
  178. padding: 0 10px;
  179. }
  180. .mbleft {
  181. width: 480px;
  182. }
  183. .mbright {
  184. width: 300px;
  185. }
  186. .catchecklist,
  187. .pagechecklist {
  188. list-style-type: none;
  189. margin: 0; padding: 0 0 10px 0;
  190. }
  191. .catchecklist li,
  192. .pagechecklist li {
  193. margin: 0; padding: 0;
  194. }
  195. .catchecklist ul {
  196. margin: 0; padding: 0 0 0 15px;
  197. }
  198. select {
  199. margin-top: 5px;
  200. }
  201. input {
  202. margin-top: 5px;
  203. }
  204. input[type="checkbox"], input[type="radio"] {
  205. margin-top: 1px;
  206. }
  207. </style>
  208.  
  209. <?php }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement