3 * Twenty Fifteen Customizer functionality
6 * @subpackage Twenty_Fifteen
7 * @since Twenty Fifteen 1.0
11 * Add postMessage support for site title and description for the Customizer.
13 * @since Twenty Fifteen 1.0
15 * @param WP_Customize_Manager $wp_customize Customizer object.
17 function twentyfifteen_customize_register( $wp_customize ) {
18 $color_scheme = twentyfifteen_get_color_scheme();
20 $wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
21 $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
23 // Add color scheme setting and control.
24 $wp_customize->add_setting( 'color_scheme', array(
25 'default' => 'default',
26 'sanitize_callback' => 'twentyfifteen_sanitize_color_scheme',
27 'transport' => 'postMessage',
30 $wp_customize->add_control( 'color_scheme', array(
31 'label' => __( 'Base Color Scheme', 'twentyfifteen' ),
32 'section' => 'colors',
34 'choices' => twentyfifteen_get_color_scheme_choices(),
38 // Add custom header and sidebar text color setting and control.
39 $wp_customize->add_setting( 'sidebar_textcolor', array(
40 'default' => $color_scheme[4],
41 'sanitize_callback' => 'sanitize_hex_color',
42 'transport' => 'postMessage',
45 $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'sidebar_textcolor', array(
46 'label' => __( 'Header and Sidebar Text Color', 'twentyfifteen' ),
47 'description' => __( 'Applied to the header on small screens and the sidebar on wide screens.', 'twentyfifteen' ),
48 'section' => 'colors',
51 // Remove the core header textcolor control, as it shares the sidebar text color.
52 $wp_customize->remove_control( 'header_textcolor' );
54 // Add custom header and sidebar background color setting and control.
55 $wp_customize->add_setting( 'header_background_color', array(
56 'default' => $color_scheme[1],
57 'sanitize_callback' => 'sanitize_hex_color',
58 'transport' => 'postMessage',
61 $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_background_color', array(
62 'label' => __( 'Header and Sidebar Background Color', 'twentyfifteen' ),
63 'description' => __( 'Applied to the header on small screens and the sidebar on wide screens.', 'twentyfifteen' ),
64 'section' => 'colors',
67 // Add an additional description to the header image section.
68 $wp_customize->get_section( 'header_image' )->description = __( 'Applied to the header on small screens and the sidebar on wide screens.', 'twentyfifteen' );
70 add_action( 'customize_register', 'twentyfifteen_customize_register', 11 );
73 * Register color schemes for Twenty Fifteen.
75 * Can be filtered with {@see 'twentyfifteen_color_schemes'}.
77 * The order of colors in a colors array:
78 * 1. Main Background Color.
79 * 2. Sidebar Background Color.
80 * 3. Box Background Color.
81 * 4. Main Text and Link Color.
82 * 5. Sidebar Text and Link Color.
83 * 6. Meta Box Background Color.
85 * @since Twenty Fifteen 1.0
87 * @return array An associative array of color scheme options.
89 function twentyfifteen_get_color_schemes() {
90 return apply_filters( 'twentyfifteen_color_schemes', array(
92 'label' => __( 'Default', 'twentyfifteen' ),
103 'label' => __( 'Dark', 'twentyfifteen' ),
114 'label' => __( 'Yellow', 'twentyfifteen' ),
125 'label' => __( 'Pink', 'twentyfifteen' ),
136 'label' => __( 'Purple', 'twentyfifteen' ),
147 'label' => __( 'Blue', 'twentyfifteen' ),
160 if ( ! function_exists( 'twentyfifteen_get_color_scheme' ) ) :
162 * Get the current Twenty Fifteen color scheme.
164 * @since Twenty Fifteen 1.0
166 * @return array An associative array of either the current or default color scheme hex values.
168 function twentyfifteen_get_color_scheme() {
169 $color_scheme_option = get_theme_mod( 'color_scheme', 'default' );
170 $color_schemes = twentyfifteen_get_color_schemes();
172 if ( array_key_exists( $color_scheme_option, $color_schemes ) ) {
173 return $color_schemes[ $color_scheme_option ]['colors'];
176 return $color_schemes['default']['colors'];
178 endif; // twentyfifteen_get_color_scheme
180 if ( ! function_exists( 'twentyfifteen_get_color_scheme_choices' ) ) :
182 * Returns an array of color scheme choices registered for Twenty Fifteen.
184 * @since Twenty Fifteen 1.0
186 * @return array Array of color schemes.
188 function twentyfifteen_get_color_scheme_choices() {
189 $color_schemes = twentyfifteen_get_color_schemes();
190 $color_scheme_control_options = array();
192 foreach ( $color_schemes as $color_scheme => $value ) {
193 $color_scheme_control_options[ $color_scheme ] = $value['label'];
196 return $color_scheme_control_options;
198 endif; // twentyfifteen_get_color_scheme_choices
200 if ( ! function_exists( 'twentyfifteen_sanitize_color_scheme' ) ) :
202 * Sanitization callback for color schemes.
204 * @since Twenty Fifteen 1.0
206 * @param string $value Color scheme name value.
207 * @return string Color scheme name.
209 function twentyfifteen_sanitize_color_scheme( $value ) {
210 $color_schemes = twentyfifteen_get_color_scheme_choices();
212 if ( ! array_key_exists( $value, $color_schemes ) ) {
218 endif; // twentyfifteen_sanitize_color_scheme
221 * Enqueues front-end CSS for color scheme.
223 * @since Twenty Fifteen 1.0
225 * @see wp_add_inline_style()
227 function twentyfifteen_color_scheme_css() {
228 $color_scheme_option = get_theme_mod( 'color_scheme', 'default' );
230 // Don't do anything if the default color scheme is selected.
231 if ( 'default' === $color_scheme_option ) {
235 $color_scheme = twentyfifteen_get_color_scheme();
237 // Convert main and sidebar text hex color to rgba.
238 $color_textcolor_rgb = twentyfifteen_hex2rgb( $color_scheme[3] );
239 $color_sidebar_textcolor_rgb = twentyfifteen_hex2rgb( $color_scheme[4] );
241 'background_color' => $color_scheme[0],
242 'header_background_color' => $color_scheme[1],
243 'box_background_color' => $color_scheme[2],
244 'textcolor' => $color_scheme[3],
245 'secondary_textcolor' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.7)', $color_textcolor_rgb ),
246 'border_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.1)', $color_textcolor_rgb ),
247 'border_focus_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.3)', $color_textcolor_rgb ),
248 'sidebar_textcolor' => $color_scheme[4],
249 'sidebar_border_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.1)', $color_sidebar_textcolor_rgb ),
250 'sidebar_border_focus_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.3)', $color_sidebar_textcolor_rgb ),
251 'secondary_sidebar_textcolor' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.7)', $color_sidebar_textcolor_rgb ),
252 'meta_box_background_color' => $color_scheme[5],
255 $color_scheme_css = twentyfifteen_get_color_scheme_css( $colors );
257 wp_add_inline_style( 'twentyfifteen-style', $color_scheme_css );
259 add_action( 'wp_enqueue_scripts', 'twentyfifteen_color_scheme_css' );
262 * Binds JS listener to make Customizer color_scheme control.
264 * Passes color scheme data as colorScheme global.
266 * @since Twenty Fifteen 1.0
268 function twentyfifteen_customize_control_js() {
269 wp_enqueue_script( 'color-scheme-control', get_template_directory_uri() . '/js/color-scheme-control.js', array( 'customize-controls', 'iris', 'underscore', 'wp-util' ), '20141216', true );
270 wp_localize_script( 'color-scheme-control', 'colorScheme', twentyfifteen_get_color_schemes() );
272 add_action( 'customize_controls_enqueue_scripts', 'twentyfifteen_customize_control_js' );
275 * Binds JS handlers to make the Customizer preview reload changes asynchronously.
277 * @since Twenty Fifteen 1.0
279 function twentyfifteen_customize_preview_js() {
280 wp_enqueue_script( 'twentyfifteen-customize-preview', get_template_directory_uri() . '/js/customize-preview.js', array( 'customize-preview' ), '20141216', true );
282 add_action( 'customize_preview_init', 'twentyfifteen_customize_preview_js' );
285 * Returns CSS for the color schemes.
287 * @since Twenty Fifteen 1.0
289 * @param array $colors Color scheme colors.
290 * @return string Color scheme CSS.
292 function twentyfifteen_get_color_scheme_css( $colors ) {
293 $colors = wp_parse_args( $colors, array(
294 'background_color' => '',
295 'header_background_color' => '',
296 'box_background_color' => '',
298 'secondary_textcolor' => '',
299 'border_color' => '',
300 'border_focus_color' => '',
301 'sidebar_textcolor' => '',
302 'sidebar_border_color' => '',
303 'sidebar_border_focus_color' => '',
304 'secondary_sidebar_textcolor' => '',
305 'meta_box_background_color' => '',
311 /* Background Color */
313 background-color: {$colors['background_color']};
316 /* Sidebar Background Color */
319 background-color: {$colors['header_background_color']};
322 /* Box Background Color */
331 background-color: {$colors['box_background_color']};
334 /* Box Background Color */
336 input[type="button"],
338 input[type="submit"],
341 .widget_calendar tbody a,
342 .widget_calendar tbody a:hover,
343 .widget_calendar tbody a:focus,
348 color: {$colors['box_background_color']};
351 /* Main Text Color */
353 input[type="button"],
355 input[type="submit"],
358 .widget_calendar tbody a,
361 background-color: {$colors['textcolor']};
364 /* Main Text Color */
369 .dropdown-toggle:after,
370 .image-navigation a:hover,
371 .image-navigation a:focus,
372 .comment-navigation a:hover,
373 .comment-navigation a:focus,
375 .entry-footer a:hover,
376 .entry-footer a:focus,
377 .comment-metadata a:hover,
378 .comment-metadata a:focus,
379 .pingback .edit-link a:hover,
380 .pingback .edit-link a:focus,
381 .comment-list .reply a:hover,
382 .comment-list .reply a:focus,
385 color: {$colors['textcolor']};
388 /* Main Text Color */
393 .pingback .comment-body > a,
394 .author-description a,
395 .taxonomy-description a,
397 .entry-footer a:hover,
398 .comment-metadata a:hover,
399 .pingback .edit-link a:hover,
400 .comment-list .reply a:hover,
402 border-color: {$colors['textcolor']};
405 /* Secondary Text Color */
408 input[type="button"]:hover,
409 input[type="button"]:focus,
410 input[type="reset"]:hover,
411 input[type="reset"]:focus,
412 input[type="submit"]:hover,
413 input[type="submit"]:focus,
414 .pagination .prev:hover,
415 .pagination .prev:focus,
416 .pagination .next:hover,
417 .pagination .next:focus,
418 .widget_calendar tbody a:hover,
419 .widget_calendar tbody a:focus,
421 .page-links a:focus {
422 background-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
423 background-color: {$colors['secondary_textcolor']};
426 /* Secondary Text Color */
430 .main-navigation .menu-item-description,
431 .post-navigation .meta-nav,
432 .post-navigation a:hover .post-title,
433 .post-navigation a:focus .post-title,
437 .comment-navigation a,
442 .taxonomy-description,
443 .page-links > .page-links-title,
448 .pingback .edit-link,
449 .pingback .edit-link a,
450 .post-password-form label,
453 .comment-awaiting-moderation,
461 .comment-list .reply a {
462 color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
463 color: {$colors['secondary_textcolor']};
466 /* Secondary Text Color */
468 .logged-in-as a:hover,
469 .comment-author a:hover {
470 border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
471 border-color: {$colors['secondary_textcolor']};
476 .dropdown-toggle:hover,
477 .dropdown-toggle:focus {
478 background-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
479 background-color: {$colors['border_color']};
493 .post-navigation div + div,
497 .widget_categories .children,
498 .widget_nav_menu .sub-menu,
499 .widget_pages .children,
504 .entry-content .page-links a,
508 .comment-list + .comment-respond,
509 .comment-list article,
510 .comment-list .pingback,
511 .comment-list .trackback,
512 .comment-list .reply a,
514 border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
515 border-color: {$colors['border_color']};
518 /* Border Focus Color */
522 outline-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
523 outline-color: {$colors['border_focus_color']};
528 border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
529 border-color: {$colors['border_focus_color']};
532 /* Sidebar Link Color */
533 .secondary-toggle:before {
534 color: {$colors['sidebar_textcolor']};
539 color: {$colors['sidebar_textcolor']};
542 /* Sidebar Text Color */
544 .site-title a:focus {
545 color: {$colors['secondary_sidebar_textcolor']};
548 /* Sidebar Border Color */
550 border-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */
551 border-color: {$colors['sidebar_border_color']};
554 /* Sidebar Border Focus Color */
555 .secondary-toggle:hover,
556 .secondary-toggle:focus {
557 border-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */
558 border-color: {$colors['sidebar_border_focus_color']};
562 outline-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */
563 outline-color: {$colors['sidebar_border_focus_color']};
566 /* Meta Background Color */
568 background-color: {$colors['meta_box_background_color']};
571 @media screen and (min-width: 38.75em) {
572 /* Main Text Color */
574 border-color: {$colors['textcolor']};
578 @media screen and (min-width: 59.6875em) {
579 /* Make sure its transparent on desktop */
582 background-color: transparent;
585 /* Sidebar Background Color */
587 .widget input[type="button"],
588 .widget input[type="reset"],
589 .widget input[type="submit"],
590 .widget_calendar tbody a,
591 .widget_calendar tbody a:hover,
592 .widget_calendar tbody a:focus {
593 color: {$colors['header_background_color']};
596 /* Sidebar Link Color */
598 .dropdown-toggle:after,
600 .widget blockquote cite,
601 .widget blockquote small {
602 color: {$colors['sidebar_textcolor']};
606 .widget input[type="button"],
607 .widget input[type="reset"],
608 .widget input[type="submit"],
609 .widget_calendar tbody a {
610 background-color: {$colors['sidebar_textcolor']};
614 border-color: {$colors['sidebar_textcolor']};
617 /* Sidebar Text Color */
620 .main-navigation .menu-item-description,
623 .widget .wp-caption-text,
624 .widget .gallery-caption {
625 color: {$colors['secondary_sidebar_textcolor']};
628 .widget button:hover,
629 .widget button:focus,
630 .widget input[type="button"]:hover,
631 .widget input[type="button"]:focus,
632 .widget input[type="reset"]:hover,
633 .widget input[type="reset"]:focus,
634 .widget input[type="submit"]:hover,
635 .widget input[type="submit"]:focus,
636 .widget_calendar tbody a:hover,
637 .widget_calendar tbody a:focus {
638 background-color: {$colors['secondary_sidebar_textcolor']};
642 border-color: {$colors['secondary_sidebar_textcolor']};
645 /* Sidebar Border Color */
655 .widget_categories .children,
656 .widget_nav_menu .sub-menu,
657 .widget_pages .children,
658 .widget abbr[title] {
659 border-color: {$colors['sidebar_border_color']};
662 .dropdown-toggle:hover,
663 .dropdown-toggle:focus,
665 background-color: {$colors['sidebar_border_color']};
669 .widget textarea:focus {
670 border-color: {$colors['sidebar_border_focus_color']};
674 .dropdown-toggle:focus {
675 outline-color: {$colors['sidebar_border_focus_color']};
684 * Output an Underscore template for generating CSS for the color scheme.
686 * The template generates the css dynamically for instant display in the Customizer
689 * @since Twenty Fifteen 1.0
691 function twentyfifteen_color_scheme_css_template() {
693 'background_color' => '{{ data.background_color }}',
694 'header_background_color' => '{{ data.header_background_color }}',
695 'box_background_color' => '{{ data.box_background_color }}',
696 'textcolor' => '{{ data.textcolor }}',
697 'secondary_textcolor' => '{{ data.secondary_textcolor }}',
698 'border_color' => '{{ data.border_color }}',
699 'border_focus_color' => '{{ data.border_focus_color }}',
700 'sidebar_textcolor' => '{{ data.sidebar_textcolor }}',
701 'sidebar_border_color' => '{{ data.sidebar_border_color }}',
702 'sidebar_border_focus_color' => '{{ data.sidebar_border_focus_color }}',
703 'secondary_sidebar_textcolor' => '{{ data.secondary_sidebar_textcolor }}',
704 'meta_box_background_color' => '{{ data.meta_box_background_color }}',
707 <script type="text/html" id="tmpl-twentyfifteen-color-scheme">
708 <?php echo twentyfifteen_get_color_scheme_css( $colors ); ?>
712 add_action( 'customize_controls_print_footer_scripts', 'twentyfifteen_color_scheme_css_template' );