3 * Twenty Eleven Theme Options
6 * @subpackage Twenty_Eleven
7 * @since Twenty Eleven 1.0
11 * Properly enqueue styles and scripts for our theme options page.
13 * This function is attached to the admin_enqueue_scripts action hook.
15 * @since Twenty Eleven 1.0
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' );
23 add_action( 'admin_print_styles-appearance_page_theme_options', 'twentyeleven_admin_enqueue_scripts' );
26 * Register the form setting for our twentyeleven_options array.
28 * This function is attached to the admin_init action hook.
30 * This call to register_setting() registers a validation callback, twentyeleven_theme_options_validate(),
31 * which is used when the option is saved, to ensure that our option values are complete, properly
32 * formatted, and safe.
34 * We also use this function to add our theme option if it doesn't already exist.
36 * @since Twenty Eleven 1.0
38 function twentyeleven_theme_options_init() {
40 // If we have no options in the database, let's add them now.
41 if ( false === twentyeleven_get_theme_options() )
42 add_option( 'twentyeleven_theme_options', twentyeleven_get_default_theme_options() );
45 'twentyeleven_options', // Options group, see settings_fields() call in theme_options_render_page()
46 'twentyeleven_theme_options', // Database option, see twentyeleven_get_theme_options()
47 'twentyeleven_theme_options_validate' // The sanitization callback, see twentyeleven_theme_options_validate()
50 add_action( 'admin_init', 'twentyeleven_theme_options_init' );
53 * Change the capability required to save the 'twentyeleven_options' options group.
55 * @see twentyeleven_theme_options_init() First parameter to register_setting() is the name of the options group.
56 * @see twentyeleven_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
58 * By default, the options groups for all registered settings require the manage_options capability.
59 * This filter is required to change our theme options page to edit_theme_options instead.
60 * By default, only administrators have either of these capabilities, but the desire here is
61 * to allow for finer-grained control for roles and users.
63 * @param string $capability The capability used for the page, which is manage_options by default.
64 * @return string The capability to actually use.
66 function twentyeleven_option_page_capability( $capability ) {
67 return 'edit_theme_options';
69 add_filter( 'option_page_capability_twentyeleven_options', 'twentyeleven_option_page_capability' );
72 * Add our theme options page to the admin menu, including some help documentation.
74 * This function is attached to the admin_menu action hook.
76 * @since Twenty Eleven 1.0
78 function twentyeleven_theme_options_add_page() {
79 $theme_page = add_theme_page(
80 __( 'Theme Options', 'twentyeleven' ), // Name of page
81 __( 'Theme Options', 'twentyeleven' ), // Label in menu
82 'edit_theme_options', // Capability required
83 'theme_options', // Menu slug, used to uniquely identify the page
84 'theme_options_render_page' // Function that renders the options page
90 $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>' .
92 '<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>' .
93 '<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>' .
94 '<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>' .
96 '<p>' . __( 'Remember to click "Save Changes" to save any changes you have made to the theme options.', 'twentyeleven' ) . '</p>' .
97 '<p><strong>' . __( 'For more information:', 'twentyeleven' ) . '</strong></p>' .
98 '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Theme_Options_Screen" target="_blank">Documentation on Theme Options</a>', 'twentyeleven' ) . '</p>' .
99 '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>', 'twentyeleven' ) . '</p>';
101 add_contextual_help( $theme_page, $help );
103 add_action( 'admin_menu', 'twentyeleven_theme_options_add_page' );
106 * Returns an array of color schemes registered for Twenty Eleven.
108 * @since Twenty Eleven 1.0
110 function twentyeleven_color_schemes() {
111 $color_scheme_options = array(
114 'label' => __( 'Light', 'twentyeleven' ),
115 'thumbnail' => get_template_directory_uri() . '/inc/images/light.png',
116 'default_link_color' => '#1b8be0',
120 'label' => __( 'Dark', 'twentyeleven' ),
121 'thumbnail' => get_template_directory_uri() . '/inc/images/dark.png',
122 'default_link_color' => '#e4741f',
126 return apply_filters( 'twentyeleven_color_schemes', $color_scheme_options );
130 * Returns an array of layout options registered for Twenty Eleven.
132 * @since Twenty Eleven 1.0
134 function twentyeleven_layouts() {
135 $layout_options = array(
136 'content-sidebar' => array(
137 'value' => 'content-sidebar',
138 'label' => __( 'Content on left', 'twentyeleven' ),
139 'thumbnail' => get_template_directory_uri() . '/inc/images/content-sidebar.png',
141 'sidebar-content' => array(
142 'value' => 'sidebar-content',
143 'label' => __( 'Content on right', 'twentyeleven' ),
144 'thumbnail' => get_template_directory_uri() . '/inc/images/sidebar-content.png',
147 'value' => 'content',
148 'label' => __( 'One-column, no sidebar', 'twentyeleven' ),
149 'thumbnail' => get_template_directory_uri() . '/inc/images/content.png',
153 return apply_filters( 'twentyeleven_layouts', $layout_options );
157 * Returns the default options for Twenty Eleven.
159 * @since Twenty Eleven 1.0
161 function twentyeleven_get_default_theme_options() {
162 $default_theme_options = array(
163 'color_scheme' => 'light',
164 'link_color' => twentyeleven_get_default_link_color( 'light' ),
165 'theme_layout' => 'content-sidebar',
169 $default_theme_options['theme_layout'] = 'sidebar-content';
171 return apply_filters( 'twentyeleven_default_theme_options', $default_theme_options );
175 * Returns the default link color for Twenty Eleven, based on color scheme.
177 * @since Twenty Eleven 1.0
179 * @param $string $color_scheme Color scheme. Defaults to the active color scheme.
180 * @return $string Color.
182 function twentyeleven_get_default_link_color( $color_scheme = null ) {
183 if ( null === $color_scheme ) {
184 $options = twentyeleven_get_theme_options();
185 $color_scheme = $options['color_scheme'];
188 $color_schemes = twentyeleven_color_schemes();
189 if ( ! isset( $color_schemes[ $color_scheme ] ) )
192 return $color_schemes[ $color_scheme ]['default_link_color'];
196 * Returns the options array for Twenty Eleven.
198 * @since Twenty Eleven 1.0
200 function twentyeleven_get_theme_options() {
201 return get_option( 'twentyeleven_theme_options', twentyeleven_get_default_theme_options() );
205 * Returns the options array for Twenty Eleven.
207 * @since Twenty Eleven 1.0
209 function theme_options_render_page() {
212 <?php screen_icon(); ?>
213 <h2><?php printf( __( '%s Theme Options', 'twentyeleven' ), get_current_theme() ); ?></h2>
214 <?php settings_errors(); ?>
216 <form method="post" action="options.php">
218 settings_fields( 'twentyeleven_options' );
219 $options = twentyeleven_get_theme_options();
220 $default_options = twentyeleven_get_default_theme_options();
223 <table class="form-table">
225 <tr valign="top" class="image-radio-option color-scheme"><th scope="row"><?php _e( 'Color Scheme', 'twentyeleven' ); ?></th>
227 <fieldset><legend class="screen-reader-text"><span><?php _e( 'Color Scheme', 'twentyeleven' ); ?></span></legend>
229 foreach ( twentyeleven_color_schemes() as $scheme ) {
232 <label class="description">
233 <input type="radio" name="twentyeleven_theme_options[color_scheme]" value="<?php echo esc_attr( $scheme['value'] ); ?>" <?php checked( $options['color_scheme'], $scheme['value'] ); ?> />
234 <input type="hidden" id="default-color-<?php echo esc_attr( $scheme['value'] ); ?>" value="<?php echo esc_attr( $scheme['default_link_color'] ); ?>" />
236 <img src="<?php echo esc_url( $scheme['thumbnail'] ); ?>" width="136" height="122" alt="" />
237 <?php echo $scheme['label']; ?>
248 <tr valign="top"><th scope="row"><?php _e( 'Link Color', 'twentyeleven' ); ?></th>
250 <fieldset><legend class="screen-reader-text"><span><?php _e( 'Link Color', 'twentyeleven' ); ?></span></legend>
251 <input type="text" name="twentyeleven_theme_options[link_color]" id="link-color" value="<?php echo esc_attr( $options['link_color'] ); ?>" />
252 <a href="#" class="pickcolor hide-if-no-js" id="link-color-example"></a>
253 <input type="button" class="pickcolor button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color', 'twentyeleven' ); ?>" />
254 <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
256 <span><?php printf( __( 'Default color: %s', 'twentyeleven' ), '<span id="default-color">' . twentyeleven_get_default_link_color( $options['color_scheme'] ) . '</span>' ); ?></span>
261 <tr valign="top" class="image-radio-option theme-layout"><th scope="row"><?php _e( 'Default Layout', 'twentyeleven' ); ?></th>
263 <fieldset><legend class="screen-reader-text"><span><?php _e( 'Color Scheme', 'twentyeleven' ); ?></span></legend>
265 foreach ( twentyeleven_layouts() as $layout ) {
268 <label class="description">
269 <input type="radio" name="twentyeleven_theme_options[theme_layout]" value="<?php echo esc_attr( $layout['value'] ); ?>" <?php checked( $options['theme_layout'], $layout['value'] ); ?> />
271 <img src="<?php echo esc_url( $layout['thumbnail'] ); ?>" width="136" height="122" alt="" />
272 <?php echo $layout['label']; ?>
284 <?php submit_button(); ?>
291 * Sanitize and validate form input. Accepts an array, return a sanitized array.
293 * @see twentyeleven_theme_options_init()
294 * @todo set up Reset Options action
296 * @since Twenty Eleven 1.0
298 function twentyeleven_theme_options_validate( $input ) {
299 $output = $defaults = twentyeleven_get_default_theme_options();
301 // Color scheme must be in our array of color scheme options
302 if ( isset( $input['color_scheme'] ) && array_key_exists( $input['color_scheme'], twentyeleven_color_schemes() ) )
303 $output['color_scheme'] = $input['color_scheme'];
305 // Our defaults for the link color may have changed, based on the color scheme.
306 $output['link_color'] = $defaults['link_color'] = twentyeleven_get_default_link_color( $output['color_scheme'] );
308 // Link color must be 3 or 6 hexadecimal characters
309 if ( isset( $input['link_color'] ) && preg_match( '/^#?([a-f0-9]{3}){1,2}$/i', $input['link_color'] ) )
310 $output['link_color'] = '#' . strtolower( ltrim( $input['link_color'], '#' ) );
312 // Theme layout must be in our array of theme layout options
313 if ( isset( $input['theme_layout'] ) && array_key_exists( $input['theme_layout'], twentyeleven_layouts() ) )
314 $output['theme_layout'] = $input['theme_layout'];
316 return apply_filters( 'twentyeleven_theme_options_validate', $output, $input, $defaults );
320 * Enqueue the styles for the current color scheme.
322 * @since Twenty Eleven 1.0
324 function twentyeleven_enqueue_color_scheme() {
325 $options = twentyeleven_get_theme_options();
326 $color_scheme = $options['color_scheme'];
328 if ( 'dark' == $color_scheme )
329 wp_enqueue_style( 'dark', get_template_directory_uri() . '/colors/dark.css', array(), null );
331 do_action( 'twentyeleven_enqueue_color_scheme', $color_scheme );
333 add_action( 'wp_enqueue_scripts', 'twentyeleven_enqueue_color_scheme' );
336 * Add a style block to the theme for the current link color.
338 * This function is attached to the wp_head action hook.
340 * @since Twenty Eleven 1.0
342 function twentyeleven_print_link_color_style() {
343 $options = twentyeleven_get_theme_options();
344 $link_color = $options['link_color'];
346 $default_options = twentyeleven_get_default_theme_options();
348 // Don't do anything if the current link color is the default.
349 if ( $default_options['link_color'] == $link_color )
357 #site-title a:active,
358 .entry-title a:hover,
359 .entry-title a:focus,
360 .entry-title a:active,
361 .widget_twentyeleven_ephemera .comments-link a:hover,
362 section.recent-posts .other-recent-posts a[rel="bookmark"]:hover,
363 section.recent-posts .other-recent-posts .comments-link a:hover,
364 .format-image footer.entry-meta a:hover,
365 #site-generator a:hover {
366 color: <?php echo $link_color; ?>;
368 section.recent-posts .other-recent-posts .comments-link a:hover {
369 border-color: <?php echo $link_color; ?>;
371 article.feature-image.small .entry-summary p a:hover,
372 .entry-header .comments-link a:hover,
373 .entry-header .comments-link a:focus,
374 .entry-header .comments-link a:active,
375 .feature-slider a.active {
376 background-color: <?php echo $link_color; ?>;
381 add_action( 'wp_head', 'twentyeleven_print_link_color_style' );
384 * Adds Twenty Eleven layout classes to the array of body classes.
386 * @since Twenty Eleven 1.0
388 function twentyeleven_layout_classes( $existing_classes ) {
389 $options = twentyeleven_get_theme_options();
390 $current_layout = $options['theme_layout'];
392 if ( in_array( $current_layout, array( 'content-sidebar', 'sidebar-content' ) ) )
393 $classes = array( 'two-column' );
395 $classes = array( 'one-column' );
397 if ( 'content-sidebar' == $current_layout )
398 $classes[] = 'right-sidebar';
399 elseif ( 'sidebar-content' == $current_layout )
400 $classes[] = 'left-sidebar';
402 $classes[] = $current_layout;
404 $classes = apply_filters( 'twentyeleven_layout_classes', $classes, $current_layout );
406 return array_merge( $existing_classes, $classes );
408 add_filter( 'body_class', 'twentyeleven_layout_classes' );