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 twentyeleven_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 // Register our settings field group
52 'general', // Unique identifier for the settings section
53 '', // Section title (we don't want one)
54 '__return_false', // Section callback (we don't want anything)
55 'theme_options' // Menu slug, used to uniquely identify the page; see twentyeleven_theme_options_add_page()
58 // Register our individual settings fields
60 'color_scheme', // Unique identifier for the field for this section
61 __( 'Color Scheme', 'twentyeleven' ), // Setting field label
62 'twentyeleven_settings_field_color_scheme', // Function that renders the settings field
63 'theme_options', // Menu slug, used to uniquely identify the page; see twentyeleven_theme_options_add_page()
64 'general' // Settings section. Same as the first argument in the add_settings_section() above
67 add_settings_field( 'link_color', __( 'Link Color', 'twentyeleven' ), 'twentyeleven_settings_field_link_color', 'theme_options', 'general' );
68 add_settings_field( 'layout', __( 'Default Layout', 'twentyeleven' ), 'twentyeleven_settings_field_layout', 'theme_options', 'general' );
70 add_action( 'admin_init', 'twentyeleven_theme_options_init' );
73 * Change the capability required to save the 'twentyeleven_options' options group.
75 * @see twentyeleven_theme_options_init() First parameter to register_setting() is the name of the options group.
76 * @see twentyeleven_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
78 * By default, the options groups for all registered settings require the manage_options capability.
79 * This filter is required to change our theme options page to edit_theme_options instead.
80 * By default, only administrators have either of these capabilities, but the desire here is
81 * to allow for finer-grained control for roles and users.
83 * @param string $capability The capability used for the page, which is manage_options by default.
84 * @return string The capability to actually use.
86 function twentyeleven_option_page_capability( $capability ) {
87 return 'edit_theme_options';
89 add_filter( 'option_page_capability_twentyeleven_options', 'twentyeleven_option_page_capability' );
92 * Add our theme options page to the admin menu, including some help documentation.
94 * This function is attached to the admin_menu action hook.
96 * @since Twenty Eleven 1.0
98 function twentyeleven_theme_options_add_page() {
99 $theme_page = add_theme_page(
100 __( 'Theme Options', 'twentyeleven' ), // Name of page
101 __( 'Theme Options', 'twentyeleven' ), // Label in menu
102 'edit_theme_options', // Capability required
103 'theme_options', // Menu slug, used to uniquely identify the page
104 'twentyeleven_theme_options_render_page' // Function that renders the options page
110 add_action( "load-$theme_page", 'twentyeleven_theme_options_help' );
112 add_action( 'admin_menu', 'twentyeleven_theme_options_add_page' );
114 function twentyeleven_theme_options_help() {
116 $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>' .
118 '<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>' .
119 '<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>' .
120 '<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>' .
122 '<p>' . __( 'Remember to click "Save Changes" to save any changes you have made to the theme options.', 'twentyeleven' ) . '</p>';
124 $sidebar = '<p><strong>' . __( 'For more information:', 'twentyeleven' ) . '</strong></p>' .
125 '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Theme_Options_Screen" target="_blank">Documentation on Theme Options</a>', 'twentyeleven' ) . '</p>' .
126 '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>', 'twentyeleven' ) . '</p>';
128 $screen = get_current_screen();
130 if ( method_exists( $screen, 'add_help_tab' ) ) {
132 $screen->add_help_tab( array(
133 'title' => __( 'Overview', 'twentyeleven' ),
134 'id' => 'theme-options-help',
139 $screen->set_help_sidebar( $sidebar );
142 add_contextual_help( $screen, $help . $sidebar );
147 * Returns an array of color schemes registered for Twenty Eleven.
149 * @since Twenty Eleven 1.0
151 function twentyeleven_color_schemes() {
152 $color_scheme_options = array(
155 'label' => __( 'Light', 'twentyeleven' ),
156 'thumbnail' => get_template_directory_uri() . '/inc/images/light.png',
157 'default_link_color' => '#1b8be0',
161 'label' => __( 'Dark', 'twentyeleven' ),
162 'thumbnail' => get_template_directory_uri() . '/inc/images/dark.png',
163 'default_link_color' => '#e4741f',
167 return apply_filters( 'twentyeleven_color_schemes', $color_scheme_options );
171 * Returns an array of layout options registered for Twenty Eleven.
173 * @since Twenty Eleven 1.0
175 function twentyeleven_layouts() {
176 $layout_options = array(
177 'content-sidebar' => array(
178 'value' => 'content-sidebar',
179 'label' => __( 'Content on left', 'twentyeleven' ),
180 'thumbnail' => get_template_directory_uri() . '/inc/images/content-sidebar.png',
182 'sidebar-content' => array(
183 'value' => 'sidebar-content',
184 'label' => __( 'Content on right', 'twentyeleven' ),
185 'thumbnail' => get_template_directory_uri() . '/inc/images/sidebar-content.png',
188 'value' => 'content',
189 'label' => __( 'One-column, no sidebar', 'twentyeleven' ),
190 'thumbnail' => get_template_directory_uri() . '/inc/images/content.png',
194 return apply_filters( 'twentyeleven_layouts', $layout_options );
198 * Returns the default options for Twenty Eleven.
200 * @since Twenty Eleven 1.0
202 function twentyeleven_get_default_theme_options() {
203 $default_theme_options = array(
204 'color_scheme' => 'light',
205 'link_color' => twentyeleven_get_default_link_color( 'light' ),
206 'theme_layout' => 'content-sidebar',
210 $default_theme_options['theme_layout'] = 'sidebar-content';
212 return apply_filters( 'twentyeleven_default_theme_options', $default_theme_options );
216 * Returns the default link color for Twenty Eleven, based on color scheme.
218 * @since Twenty Eleven 1.0
220 * @param $string $color_scheme Color scheme. Defaults to the active color scheme.
221 * @return $string Color.
223 function twentyeleven_get_default_link_color( $color_scheme = null ) {
224 if ( null === $color_scheme ) {
225 $options = twentyeleven_get_theme_options();
226 $color_scheme = $options['color_scheme'];
229 $color_schemes = twentyeleven_color_schemes();
230 if ( ! isset( $color_schemes[ $color_scheme ] ) )
233 return $color_schemes[ $color_scheme ]['default_link_color'];
237 * Returns the options array for Twenty Eleven.
239 * @since Twenty Eleven 1.0
241 function twentyeleven_get_theme_options() {
242 return get_option( 'twentyeleven_theme_options', twentyeleven_get_default_theme_options() );
246 * Renders the Color Scheme setting field.
248 * @since Twenty Eleven 1.3
250 function twentyeleven_settings_field_color_scheme() {
251 $options = twentyeleven_get_theme_options();
253 foreach ( twentyeleven_color_schemes() as $scheme ) {
255 <div class="layout image-radio-option color-scheme">
256 <label class="description">
257 <input type="radio" name="twentyeleven_theme_options[color_scheme]" value="<?php echo esc_attr( $scheme['value'] ); ?>" <?php checked( $options['color_scheme'], $scheme['value'] ); ?> />
258 <input type="hidden" id="default-color-<?php echo esc_attr( $scheme['value'] ); ?>" value="<?php echo esc_attr( $scheme['default_link_color'] ); ?>" />
260 <img src="<?php echo esc_url( $scheme['thumbnail'] ); ?>" width="136" height="122" alt="" />
261 <?php echo $scheme['label']; ?>
270 * Renders the Link Color setting field.
272 * @since Twenty Eleven 1.3
274 function twentyeleven_settings_field_link_color() {
275 $options = twentyeleven_get_theme_options();
277 <input type="text" name="twentyeleven_theme_options[link_color]" id="link-color" value="<?php echo esc_attr( $options['link_color'] ); ?>" />
278 <a href="#" class="pickcolor hide-if-no-js" id="link-color-example"></a>
279 <input type="button" class="pickcolor button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color', 'twentyeleven' ); ?>" />
280 <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
282 <span><?php printf( __( 'Default color: %s', 'twentyeleven' ), '<span id="default-color">' . twentyeleven_get_default_link_color( $options['color_scheme'] ) . '</span>' ); ?></span>
287 * Renders the Layout setting field.
289 * @since Twenty Eleven 1.3
291 function twentyeleven_settings_field_layout() {
292 $options = twentyeleven_get_theme_options();
293 foreach ( twentyeleven_layouts() as $layout ) {
295 <div class="layout image-radio-option theme-layout">
296 <label class="description">
297 <input type="radio" name="twentyeleven_theme_options[theme_layout]" value="<?php echo esc_attr( $layout['value'] ); ?>" <?php checked( $options['theme_layout'], $layout['value'] ); ?> />
299 <img src="<?php echo esc_url( $layout['thumbnail'] ); ?>" width="136" height="122" alt="" />
300 <?php echo $layout['label']; ?>
309 * Returns the options array for Twenty Eleven.
311 * @since Twenty Eleven 1.2
313 function twentyeleven_theme_options_render_page() {
316 <?php screen_icon(); ?>
317 <h2><?php printf( __( '%s Theme Options', 'twentyeleven' ), get_current_theme() ); ?></h2>
318 <?php settings_errors(); ?>
320 <form method="post" action="options.php">
322 settings_fields( 'twentyeleven_options' );
323 do_settings_sections( 'theme_options' );
332 * Sanitize and validate form input. Accepts an array, return a sanitized array.
334 * @see twentyeleven_theme_options_init()
335 * @todo set up Reset Options action
337 * @since Twenty Eleven 1.0
339 function twentyeleven_theme_options_validate( $input ) {
340 $output = $defaults = twentyeleven_get_default_theme_options();
342 // Color scheme must be in our array of color scheme options
343 if ( isset( $input['color_scheme'] ) && array_key_exists( $input['color_scheme'], twentyeleven_color_schemes() ) )
344 $output['color_scheme'] = $input['color_scheme'];
346 // Our defaults for the link color may have changed, based on the color scheme.
347 $output['link_color'] = $defaults['link_color'] = twentyeleven_get_default_link_color( $output['color_scheme'] );
349 // Link color must be 3 or 6 hexadecimal characters
350 if ( isset( $input['link_color'] ) && preg_match( '/^#?([a-f0-9]{3}){1,2}$/i', $input['link_color'] ) )
351 $output['link_color'] = '#' . strtolower( ltrim( $input['link_color'], '#' ) );
353 // Theme layout must be in our array of theme layout options
354 if ( isset( $input['theme_layout'] ) && array_key_exists( $input['theme_layout'], twentyeleven_layouts() ) )
355 $output['theme_layout'] = $input['theme_layout'];
357 return apply_filters( 'twentyeleven_theme_options_validate', $output, $input, $defaults );
361 * Enqueue the styles for the current color scheme.
363 * @since Twenty Eleven 1.0
365 function twentyeleven_enqueue_color_scheme() {
366 $options = twentyeleven_get_theme_options();
367 $color_scheme = $options['color_scheme'];
369 if ( 'dark' == $color_scheme )
370 wp_enqueue_style( 'dark', get_template_directory_uri() . '/colors/dark.css', array(), null );
372 do_action( 'twentyeleven_enqueue_color_scheme', $color_scheme );
374 add_action( 'wp_enqueue_scripts', 'twentyeleven_enqueue_color_scheme' );
377 * Add a style block to the theme for the current link color.
379 * This function is attached to the wp_head action hook.
381 * @since Twenty Eleven 1.0
383 function twentyeleven_print_link_color_style() {
384 $options = twentyeleven_get_theme_options();
385 $link_color = $options['link_color'];
387 $default_options = twentyeleven_get_default_theme_options();
389 // Don't do anything if the current link color is the default.
390 if ( $default_options['link_color'] == $link_color )
398 #site-title a:active,
399 .entry-title a:hover,
400 .entry-title a:focus,
401 .entry-title a:active,
402 .widget_twentyeleven_ephemera .comments-link a:hover,
403 section.recent-posts .other-recent-posts a[rel="bookmark"]:hover,
404 section.recent-posts .other-recent-posts .comments-link a:hover,
405 .format-image footer.entry-meta a:hover,
406 #site-generator a:hover {
407 color: <?php echo $link_color; ?>;
409 section.recent-posts .other-recent-posts .comments-link a:hover {
410 border-color: <?php echo $link_color; ?>;
412 article.feature-image.small .entry-summary p a:hover,
413 .entry-header .comments-link a:hover,
414 .entry-header .comments-link a:focus,
415 .entry-header .comments-link a:active,
416 .feature-slider a.active {
417 background-color: <?php echo $link_color; ?>;
422 add_action( 'wp_head', 'twentyeleven_print_link_color_style' );
425 * Adds Twenty Eleven layout classes to the array of body classes.
427 * @since Twenty Eleven 1.0
429 function twentyeleven_layout_classes( $existing_classes ) {
430 $options = twentyeleven_get_theme_options();
431 $current_layout = $options['theme_layout'];
433 if ( in_array( $current_layout, array( 'content-sidebar', 'sidebar-content' ) ) )
434 $classes = array( 'two-column' );
436 $classes = array( 'one-column' );
438 if ( 'content-sidebar' == $current_layout )
439 $classes[] = 'right-sidebar';
440 elseif ( 'sidebar-content' == $current_layout )
441 $classes[] = 'left-sidebar';
443 $classes[] = $current_layout;
445 $classes = apply_filters( 'twentyeleven_layout_classes', $classes, $current_layout );
447 return array_merge( $existing_classes, $classes );
449 add_filter( 'body_class', 'twentyeleven_layout_classes' );