/inc/theme-options.php

  1. <?php 
  2. /** 
  3. * Twenty Eleven Theme Options 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Twenty_Eleven 
  7. * @since Twenty Eleven 1.0 
  8. */ 
  9.  
  10. /** 
  11. * Properly enqueue styles and scripts for our theme options page. 
  12. * 
  13. * This function is attached to the admin_enqueue_scripts action hook. 
  14. * 
  15. * @since Twenty Eleven 1.0 
  16. * 
  17. */ 
  18. function twentyeleven_admin_enqueue_scripts( $hook_suffix ) { 
  19. wp_enqueue_style( 'twentyeleven-theme-options', get_template_directory_uri() . '/inc/theme-options.css', false, '2011-04-28' ); 
  20. wp_enqueue_script( 'twentyeleven-theme-options', get_template_directory_uri() . '/inc/theme-options.js', array( 'farbtastic' ), '2011-06-10' ); 
  21. wp_enqueue_style( 'farbtastic' ); 
  22. add_action( 'admin_print_styles-appearance_page_theme_options', 'twentyeleven_admin_enqueue_scripts' ); 
  23.  
  24. /** 
  25. * Register the form setting for our twentyeleven_options array. 
  26. * 
  27. * This function is attached to the admin_init action hook. 
  28. * 
  29. * This call to register_setting() registers a validation callback, twentyeleven_theme_options_validate(),  
  30. * which is used when the option is saved, to ensure that our option values are complete, properly 
  31. * formatted, and safe. 
  32. * 
  33. * We also use this function to add our theme option if it doesn't already exist. 
  34. * 
  35. * @since Twenty Eleven 1.0 
  36. */ 
  37. function twentyeleven_theme_options_init() { 
  38.  
  39. // If we have no options in the database, let's add them now. 
  40. if ( false === twentyeleven_get_theme_options() ) 
  41. add_option( 'twentyeleven_theme_options', twentyeleven_get_default_theme_options() ); 
  42.  
  43. register_setting( 
  44. 'twentyeleven_options', // Options group, see settings_fields() call in theme_options_render_page() 
  45. 'twentyeleven_theme_options', // Database option, see twentyeleven_get_theme_options() 
  46. 'twentyeleven_theme_options_validate' // The sanitization callback, see twentyeleven_theme_options_validate() 
  47. ); 
  48. add_action( 'admin_init', 'twentyeleven_theme_options_init' ); 
  49.  
  50. /** 
  51. * Change the capability required to save the 'twentyeleven_options' options group. 
  52. * 
  53. * @see twentyeleven_theme_options_init() First parameter to register_setting() is the name of the options group. 
  54. * @see twentyeleven_theme_options_add_page() The edit_theme_options capability is used for viewing the page. 
  55. * 
  56. * By default, the options groups for all registered settings require the manage_options capability. 
  57. * This filter is required to change our theme options page to edit_theme_options instead. 
  58. * By default, only administrators have either of these capabilities, but the desire here is 
  59. * to allow for finer-grained control for roles and users. 
  60. * 
  61. * @param string $capability The capability used for the page, which is manage_options by default. 
  62. * @return string The capability to actually use. 
  63. */ 
  64. function twentyeleven_option_page_capability( $capability ) { 
  65. return 'edit_theme_options'; 
  66. add_filter( 'option_page_capability_twentyeleven_options', 'twentyeleven_option_page_capability' ); 
  67.  
  68. /** 
  69. * Add our theme options page to the admin menu, including some help documentation. 
  70. * 
  71. * This function is attached to the admin_menu action hook. 
  72. * 
  73. * @since Twenty Eleven 1.0 
  74. */ 
  75. function twentyeleven_theme_options_add_page() { 
  76. $theme_page = add_theme_page( 
  77. __( 'Theme Options', 'twentyeleven' ), // Name of page 
  78. __( 'Theme Options', 'twentyeleven' ), // Label in menu 
  79. 'edit_theme_options', // Capability required 
  80. 'theme_options', // Menu slug, used to uniquely identify the page 
  81. 'twentyeleven_theme_options_render_page' // Function that renders the options page 
  82. ); 
  83.  
  84. if ( ! $theme_page ) 
  85. return; 
  86.  
  87. $help = '<p>' . __( 'Some themes provide customization options that are grouped together on a Theme Options screen. If you change themes, options may change or disappear, as they are theme-specific. Your current theme, Twenty Eleven, provides the following Theme Options:', 'twentyeleven' ) . '</p>' . 
  88. '<ol>' . 
  89. '<li>' . __( '<strong>Color Scheme</strong>: You can choose a color palette of "Light" (light background with dark text) or "Dark" (dark background with light text) for your site.', 'twentyeleven' ) . '</li>' . 
  90. '<li>' . __( '<strong>Link Color</strong>: You can choose the color used for text links on your site. You can enter the HTML color or hex code, or you can choose visually by clicking the "Select a Color" button to pick from a color wheel.', 'twentyeleven' ) . '</li>' . 
  91. '<li>' . __( '<strong>Default Layout</strong>: You can choose if you want your site’s default layout to have a sidebar on the left, the right, or not at all.', 'twentyeleven' ) . '</li>' . 
  92. '</ol>' . 
  93. '<p>' . __( 'Remember to click "Save Changes" to save any changes you have made to the theme options.', 'twentyeleven' ) . '</p>' . 
  94. '<p><strong>' . __( 'For more information:', 'twentyeleven' ) . '</strong></p>' . 
  95. '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Theme_Options_Screen" target="_blank">Documentation on Theme Options</a>', 'twentyeleven' ) . '</p>' . 
  96. '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>', 'twentyeleven' ) . '</p>'; 
  97.  
  98. add_contextual_help( $theme_page, $help ); 
  99. add_action( 'admin_menu', 'twentyeleven_theme_options_add_page' ); 
  100.  
  101. /** 
  102. * Returns an array of color schemes registered for Twenty Eleven. 
  103. * 
  104. * @since Twenty Eleven 1.0 
  105. */ 
  106. function twentyeleven_color_schemes() { 
  107. $color_scheme_options = array( 
  108. 'light' => array( 
  109. 'value' => 'light',  
  110. 'label' => __( 'Light', 'twentyeleven' ),  
  111. 'thumbnail' => get_template_directory_uri() . '/inc/images/light.png',  
  112. 'default_link_color' => '#1b8be0',  
  113. ),  
  114. 'dark' => array( 
  115. 'value' => 'dark',  
  116. 'label' => __( 'Dark', 'twentyeleven' ),  
  117. 'thumbnail' => get_template_directory_uri() . '/inc/images/dark.png',  
  118. 'default_link_color' => '#e4741f',  
  119. ),  
  120. ); 
  121.  
  122. return apply_filters( 'twentyeleven_color_schemes', $color_scheme_options ); 
  123.  
  124. /** 
  125. * Returns an array of layout options registered for Twenty Eleven. 
  126. * 
  127. * @since Twenty Eleven 1.0 
  128. */ 
  129. function twentyeleven_layouts() { 
  130. $layout_options = array( 
  131. 'content-sidebar' => array( 
  132. 'value' => 'content-sidebar',  
  133. 'label' => __( 'Content on left', 'twentyeleven' ),  
  134. 'thumbnail' => get_template_directory_uri() . '/inc/images/content-sidebar.png',  
  135. ),  
  136. 'sidebar-content' => array( 
  137. 'value' => 'sidebar-content',  
  138. 'label' => __( 'Content on right', 'twentyeleven' ),  
  139. 'thumbnail' => get_template_directory_uri() . '/inc/images/sidebar-content.png',  
  140. ),  
  141. 'content' => array( 
  142. 'value' => 'content',  
  143. 'label' => __( 'One-column, no sidebar', 'twentyeleven' ),  
  144. 'thumbnail' => get_template_directory_uri() . '/inc/images/content.png',  
  145. ),  
  146. ); 
  147.  
  148. return apply_filters( 'twentyeleven_layouts', $layout_options ); 
  149.  
  150. /** 
  151. * Returns the default options for Twenty Eleven. 
  152. * 
  153. * @since Twenty Eleven 1.0 
  154. */ 
  155. function twentyeleven_get_default_theme_options() { 
  156. $default_theme_options = array( 
  157. 'color_scheme' => 'light',  
  158. 'link_color' => twentyeleven_get_default_link_color( 'light' ),  
  159. 'theme_layout' => 'content-sidebar',  
  160. ); 
  161.  
  162. if ( is_rtl() ) 
  163. $default_theme_options['theme_layout'] = 'sidebar-content'; 
  164.  
  165. return apply_filters( 'twentyeleven_default_theme_options', $default_theme_options ); 
  166.  
  167. /** 
  168. * Returns the default link color for Twenty Eleven, based on color scheme. 
  169. * 
  170. * @since Twenty Eleven 1.0 
  171. * 
  172. * @param $string $color_scheme Color scheme. Defaults to the active color scheme. 
  173. * @return $string Color. 
  174. */ 
  175. function twentyeleven_get_default_link_color( $color_scheme = null ) { 
  176. if ( null === $color_scheme ) { 
  177. $options = twentyeleven_get_theme_options(); 
  178. $color_scheme = $options['color_scheme']; 
  179.  
  180. $color_schemes = twentyeleven_color_schemes(); 
  181. if ( ! isset( $color_schemes[ $color_scheme ] ) ) 
  182. return false; 
  183.  
  184. return $color_schemes[ $color_scheme ]['default_link_color']; 
  185.  
  186. /** 
  187. * Returns the options array for Twenty Eleven. 
  188. * 
  189. * @since Twenty Eleven 1.0 
  190. */ 
  191. function twentyeleven_get_theme_options() { 
  192. return get_option( 'twentyeleven_theme_options', twentyeleven_get_default_theme_options() ); 
  193.  
  194. /** 
  195. * Returns the options array for Twenty Eleven. 
  196. * 
  197. * @since Twenty Eleven 1.2 
  198. */ 
  199. function twentyeleven_theme_options_render_page() { 
  200. ?> 
  201. <div class="wrap"> 
  202. <?php screen_icon(); ?> 
  203. <h2><?php printf( __( '%s Theme Options', 'twentyeleven' ), get_current_theme() ); ?></h2> 
  204. <?php settings_errors(); ?> 
  205.  
  206. <form method="post" action="options.php"> 
  207. <?php 
  208. settings_fields( 'twentyeleven_options' ); 
  209. $options = twentyeleven_get_theme_options(); 
  210. $default_options = twentyeleven_get_default_theme_options(); 
  211. ?> 
  212.  
  213. <table class="form-table"> 
  214.  
  215. <tr valign="top" class="image-radio-option color-scheme"><th scope="row"><?php _e( 'Color Scheme', 'twentyeleven' ); ?></th> 
  216. <td> 
  217. <fieldset><legend class="screen-reader-text"><span><?php _e( 'Color Scheme', 'twentyeleven' ); ?></span></legend> 
  218. <?php 
  219. foreach ( twentyeleven_color_schemes() as $scheme ) { 
  220. ?> 
  221. <div class="layout"> 
  222. <label class="description"> 
  223. <input type="radio" name="twentyeleven_theme_options[color_scheme]" value="<?php echo esc_attr( $scheme['value'] ); ?>" <?php checked( $options['color_scheme'], $scheme['value'] ); ?> /> 
  224. <input type="hidden" id="default-color-<?php echo esc_attr( $scheme['value'] ); ?>" value="<?php echo esc_attr( $scheme['default_link_color'] ); ?>" /> 
  225. <span> 
  226. <img src="<?php echo esc_url( $scheme['thumbnail'] ); ?>" width="136" height="122" alt="" /> 
  227. <?php echo $scheme['label']; ?> 
  228. </span> 
  229. </label> 
  230. </div> 
  231. <?php 
  232. ?> 
  233. </fieldset> 
  234. </td> 
  235. </tr> 
  236.  
  237. <tr valign="top"><th scope="row"><?php _e( 'Link Color', 'twentyeleven' ); ?></th> 
  238. <td> 
  239. <fieldset><legend class="screen-reader-text"><span><?php _e( 'Link Color', 'twentyeleven' ); ?></span></legend> 
  240. <input type="text" name="twentyeleven_theme_options[link_color]" id="link-color" value="<?php echo esc_attr( $options['link_color'] ); ?>" /> 
  241. <a href="#" class="pickcolor hide-if-no-js" id="link-color-example"></a> 
  242. <input type="button" class="pickcolor button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color', 'twentyeleven' ); ?>" /> 
  243. <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div> 
  244. <br /> 
  245. <span><?php printf( __( 'Default color: %s', 'twentyeleven' ), '<span id="default-color">' . twentyeleven_get_default_link_color( $options['color_scheme'] ) . '</span>' ); ?></span> 
  246. </fieldset> 
  247. </td> 
  248. </tr> 
  249.  
  250. <tr valign="top" class="image-radio-option theme-layout"><th scope="row"><?php _e( 'Default Layout', 'twentyeleven' ); ?></th> 
  251. <td> 
  252. <fieldset><legend class="screen-reader-text"><span><?php _e( 'Color Scheme', 'twentyeleven' ); ?></span></legend> 
  253. <?php 
  254. foreach ( twentyeleven_layouts() as $layout ) { 
  255. ?> 
  256. <div class="layout"> 
  257. <label class="description"> 
  258. <input type="radio" name="twentyeleven_theme_options[theme_layout]" value="<?php echo esc_attr( $layout['value'] ); ?>" <?php checked( $options['theme_layout'], $layout['value'] ); ?> /> 
  259. <span> 
  260. <img src="<?php echo esc_url( $layout['thumbnail'] ); ?>" width="136" height="122" alt="" /> 
  261. <?php echo $layout['label']; ?> 
  262. </span> 
  263. </label> 
  264. </div> 
  265. <?php 
  266. ?> 
  267. </fieldset> 
  268. </td> 
  269. </tr> 
  270. </table> 
  271.  
  272. <?php submit_button(); ?> 
  273. </form> 
  274. </div> 
  275. <?php 
  276.  
  277. /** 
  278. * Sanitize and validate form input. Accepts an array, return a sanitized array. 
  279. * 
  280. * @see twentyeleven_theme_options_init() 
  281. * @todo set up Reset Options action 
  282. * 
  283. * @since Twenty Eleven 1.0 
  284. */ 
  285. function twentyeleven_theme_options_validate( $input ) { 
  286. $output = $defaults = twentyeleven_get_default_theme_options(); 
  287.  
  288. // Color scheme must be in our array of color scheme options 
  289. if ( isset( $input['color_scheme'] ) && array_key_exists( $input['color_scheme'], twentyeleven_color_schemes() ) ) 
  290. $output['color_scheme'] = $input['color_scheme']; 
  291.  
  292. // Our defaults for the link color may have changed, based on the color scheme. 
  293. $output['link_color'] = $defaults['link_color'] = twentyeleven_get_default_link_color( $output['color_scheme'] ); 
  294.  
  295. // Link color must be 3 or 6 hexadecimal characters 
  296. if ( isset( $input['link_color'] ) && preg_match( '/^#?([a-f0-9]{3}) {1, 2}$/i', $input['link_color'] ) ) 
  297. $output['link_color'] = '#' . strtolower( ltrim( $input['link_color'], '#' ) ); 
  298.  
  299. // Theme layout must be in our array of theme layout options 
  300. if ( isset( $input['theme_layout'] ) && array_key_exists( $input['theme_layout'], twentyeleven_layouts() ) ) 
  301. $output['theme_layout'] = $input['theme_layout']; 
  302.  
  303. return apply_filters( 'twentyeleven_theme_options_validate', $output, $input, $defaults ); 
  304.  
  305. /** 
  306. * Enqueue the styles for the current color scheme. 
  307. * 
  308. * @since Twenty Eleven 1.0 
  309. */ 
  310. function twentyeleven_enqueue_color_scheme() { 
  311. $options = twentyeleven_get_theme_options(); 
  312. $color_scheme = $options['color_scheme']; 
  313.  
  314. if ( 'dark' == $color_scheme ) 
  315. wp_enqueue_style( 'dark', get_template_directory_uri() . '/colors/dark.css', array(), null ); 
  316.  
  317. do_action( 'twentyeleven_enqueue_color_scheme', $color_scheme ); 
  318. add_action( 'wp_enqueue_scripts', 'twentyeleven_enqueue_color_scheme' ); 
  319.  
  320. /** 
  321. * Add a style block to the theme for the current link color. 
  322. * 
  323. * This function is attached to the wp_head action hook. 
  324. * 
  325. * @since Twenty Eleven 1.0 
  326. */ 
  327. function twentyeleven_print_link_color_style() { 
  328. $options = twentyeleven_get_theme_options(); 
  329. $link_color = $options['link_color']; 
  330.  
  331. $default_options = twentyeleven_get_default_theme_options(); 
  332.  
  333. // Don't do anything if the current link color is the default. 
  334. if ( $default_options['link_color'] == $link_color ) 
  335. return; 
  336. ?> 
  337. <style> 
  338. /* Link color */ 
  339. a,  
  340. #site-title a:focus,  
  341. #site-title a:hover,  
  342. #site-title a:active,  
  343. .entry-title a:hover,  
  344. .entry-title a:focus,  
  345. .entry-title a:active,  
  346. .widget_twentyeleven_ephemera .comments-link a:hover,  
  347. section.recent-posts .other-recent-posts a[rel="bookmark"]:hover,  
  348. section.recent-posts .other-recent-posts .comments-link a:hover,  
  349. .format-image footer.entry-meta a:hover,  
  350. #site-generator a:hover { 
  351. color: <?php echo $link_color; ?>; 
  352. section.recent-posts .other-recent-posts .comments-link a:hover { 
  353. border-color: <?php echo $link_color; ?>; 
  354. article.feature-image.small .entry-summary p a:hover,  
  355. .entry-header .comments-link a:hover,  
  356. .entry-header .comments-link a:focus,  
  357. .entry-header .comments-link a:active,  
  358. .feature-slider a.active { 
  359. background-color: <?php echo $link_color; ?>; 
  360. </style> 
  361. <?php 
  362. add_action( 'wp_head', 'twentyeleven_print_link_color_style' ); 
  363.  
  364. /** 
  365. * Adds Twenty Eleven layout classes to the array of body classes. 
  366. * 
  367. * @since Twenty Eleven 1.0 
  368. */ 
  369. function twentyeleven_layout_classes( $existing_classes ) { 
  370. $options = twentyeleven_get_theme_options(); 
  371. $current_layout = $options['theme_layout']; 
  372.  
  373. if ( in_array( $current_layout, array( 'content-sidebar', 'sidebar-content' ) ) ) 
  374. $classes = array( 'two-column' ); 
  375. else 
  376. $classes = array( 'one-column' ); 
  377.  
  378. if ( 'content-sidebar' == $current_layout ) 
  379. $classes[] = 'right-sidebar'; 
  380. elseif ( 'sidebar-content' == $current_layout ) 
  381. $classes[] = 'left-sidebar'; 
  382. else 
  383. $classes[] = $current_layout; 
  384.  
  385. $classes = apply_filters( 'twentyeleven_layout_classes', $classes, $current_layout ); 
  386.  
  387. return array_merge( $existing_classes, $classes ); 
  388. add_filter( 'body_class', 'twentyeleven_layout_classes' ); 
.