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() {
91 * Filter the color schemes registered for use with Twenty Fifteen.
93 * The default schemes include 'default', 'dark', 'yellow', 'pink', 'purple', and 'blue'.
95 * @since Twenty Fifteen 1.0
97 * @param array $schemes {
98 * Associative array of color schemes data.
100 * @type array $slug {
101 * Associative array of information for setting up the color scheme.
103 * @type string $label Color scheme label.
104 * @type array $colors HEX codes for default colors prepended with a hash symbol ('#').
105 * Colors are defined in the following order: Main background, sidebar
106 * background, box background, main text and link, sidebar text and link,
107 * meta box background.
111 return apply_filters( 'twentyfifteen_color_schemes', array(
113 'label' => __( 'Default', 'twentyfifteen' ),
124 'label' => __( 'Dark', 'twentyfifteen' ),
135 'label' => __( 'Yellow', 'twentyfifteen' ),
146 'label' => __( 'Pink', 'twentyfifteen' ),
157 'label' => __( 'Purple', 'twentyfifteen' ),
168 'label' => __( 'Blue', 'twentyfifteen' ),
181 if ( ! function_exists( 'twentyfifteen_get_color_scheme' ) ) :
183 * Get the current Twenty Fifteen color scheme.
185 * @since Twenty Fifteen 1.0
187 * @return array An associative array of either the current or default color scheme hex values.
189 function twentyfifteen_get_color_scheme() {
190 $color_scheme_option = get_theme_mod( 'color_scheme', 'default' );
191 $color_schemes = twentyfifteen_get_color_schemes();
193 if ( array_key_exists( $color_scheme_option, $color_schemes ) ) {
194 return $color_schemes[ $color_scheme_option ]['colors'];
197 return $color_schemes['default']['colors'];
199 endif; // twentyfifteen_get_color_scheme
201 if ( ! function_exists( 'twentyfifteen_get_color_scheme_choices' ) ) :
203 * Returns an array of color scheme choices registered for Twenty Fifteen.
205 * @since Twenty Fifteen 1.0
207 * @return array Array of color schemes.
209 function twentyfifteen_get_color_scheme_choices() {
210 $color_schemes = twentyfifteen_get_color_schemes();
211 $color_scheme_control_options = array();
213 foreach ( $color_schemes as $color_scheme => $value ) {
214 $color_scheme_control_options[ $color_scheme ] = $value['label'];
217 return $color_scheme_control_options;
219 endif; // twentyfifteen_get_color_scheme_choices
221 if ( ! function_exists( 'twentyfifteen_sanitize_color_scheme' ) ) :
223 * Sanitization callback for color schemes.
225 * @since Twenty Fifteen 1.0
227 * @param string $value Color scheme name value.
228 * @return string Color scheme name.
230 function twentyfifteen_sanitize_color_scheme( $value ) {
231 $color_schemes = twentyfifteen_get_color_scheme_choices();
233 if ( ! array_key_exists( $value, $color_schemes ) ) {
239 endif; // twentyfifteen_sanitize_color_scheme
242 * Enqueues front-end CSS for color scheme.
244 * @since Twenty Fifteen 1.0
246 * @see wp_add_inline_style()
248 function twentyfifteen_color_scheme_css() {
249 $color_scheme_option = get_theme_mod( 'color_scheme', 'default' );
251 // Don't do anything if the default color scheme is selected.
252 if ( 'default' === $color_scheme_option ) {
256 $color_scheme = twentyfifteen_get_color_scheme();
258 // Convert main and sidebar text hex color to rgba.
259 $color_textcolor_rgb = twentyfifteen_hex2rgb( $color_scheme[3] );
260 $color_sidebar_textcolor_rgb = twentyfifteen_hex2rgb( $color_scheme[4] );
262 'background_color' => $color_scheme[0],
263 'header_background_color' => $color_scheme[1],
264 'box_background_color' => $color_scheme[2],
265 'textcolor' => $color_scheme[3],
266 'secondary_textcolor' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.7)', $color_textcolor_rgb ),
267 'border_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.1)', $color_textcolor_rgb ),
268 'border_focus_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.3)', $color_textcolor_rgb ),
269 'sidebar_textcolor' => $color_scheme[4],
270 'sidebar_border_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.1)', $color_sidebar_textcolor_rgb ),
271 'sidebar_border_focus_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.3)', $color_sidebar_textcolor_rgb ),
272 'secondary_sidebar_textcolor' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.7)', $color_sidebar_textcolor_rgb ),
273 'meta_box_background_color' => $color_scheme[5],
276 $color_scheme_css = twentyfifteen_get_color_scheme_css( $colors );
278 wp_add_inline_style( 'twentyfifteen-style', $color_scheme_css );
280 add_action( 'wp_enqueue_scripts', 'twentyfifteen_color_scheme_css' );
283 * Binds JS listener to make Customizer color_scheme control.
285 * Passes color scheme data as colorScheme global.
287 * @since Twenty Fifteen 1.0
289 function twentyfifteen_customize_control_js() {
290 wp_enqueue_script( 'color-scheme-control', get_template_directory_uri() . '/js/color-scheme-control.js', array( 'customize-controls', 'iris', 'underscore', 'wp-util' ), '20141216', true );
291 wp_localize_script( 'color-scheme-control', 'colorScheme', twentyfifteen_get_color_schemes() );
293 add_action( 'customize_controls_enqueue_scripts', 'twentyfifteen_customize_control_js' );
296 * Binds JS handlers to make the Customizer preview reload changes asynchronously.
298 * @since Twenty Fifteen 1.0
300 function twentyfifteen_customize_preview_js() {
301 wp_enqueue_script( 'twentyfifteen-customize-preview', get_template_directory_uri() . '/js/customize-preview.js', array( 'customize-preview' ), '20141216', true );
303 add_action( 'customize_preview_init', 'twentyfifteen_customize_preview_js' );
306 * Returns CSS for the color schemes.
308 * @since Twenty Fifteen 1.0
310 * @param array $colors Color scheme colors.
311 * @return string Color scheme CSS.
313 function twentyfifteen_get_color_scheme_css( $colors ) {
314 $colors = wp_parse_args( $colors, array(
315 'background_color' => '',
316 'header_background_color' => '',
317 'box_background_color' => '',
319 'secondary_textcolor' => '',
320 'border_color' => '',
321 'border_focus_color' => '',
322 'sidebar_textcolor' => '',
323 'sidebar_border_color' => '',
324 'sidebar_border_focus_color' => '',
325 'secondary_sidebar_textcolor' => '',
326 'meta_box_background_color' => '',
332 /* Background Color */
334 background-color: {$colors['background_color']};
337 /* Sidebar Background Color */
340 background-color: {$colors['header_background_color']};
343 /* Box Background Color */
353 background-color: {$colors['box_background_color']};
356 /* Box Background Color */
358 input[type="button"],
360 input[type="submit"],
363 .widget_calendar tbody a,
364 .widget_calendar tbody a:hover,
365 .widget_calendar tbody a:focus,
370 color: {$colors['box_background_color']};
373 /* Main Text Color */
375 input[type="button"],
377 input[type="submit"],
380 .widget_calendar tbody a,
383 background-color: {$colors['textcolor']};
386 /* Main Text Color */
391 .dropdown-toggle:after,
392 .image-navigation a:hover,
393 .image-navigation a:focus,
394 .comment-navigation a:hover,
395 .comment-navigation a:focus,
397 .entry-footer a:hover,
398 .entry-footer a:focus,
399 .comment-metadata a:hover,
400 .comment-metadata a:focus,
401 .pingback .edit-link a:hover,
402 .pingback .edit-link a:focus,
403 .comment-list .reply a:hover,
404 .comment-list .reply a:focus,
407 color: {$colors['textcolor']};
410 /* Main Text Color */
415 .pingback .comment-body > a,
416 .author-description a,
417 .taxonomy-description a,
419 .entry-footer a:hover,
420 .comment-metadata a:hover,
421 .pingback .edit-link a:hover,
422 .comment-list .reply a:hover,
424 border-color: {$colors['textcolor']};
427 /* Secondary Text Color */
430 input[type="button"]:hover,
431 input[type="button"]:focus,
432 input[type="reset"]:hover,
433 input[type="reset"]:focus,
434 input[type="submit"]:hover,
435 input[type="submit"]:focus,
436 .pagination .prev:hover,
437 .pagination .prev:focus,
438 .pagination .next:hover,
439 .pagination .next:focus,
440 .widget_calendar tbody a:hover,
441 .widget_calendar tbody a:focus,
443 .page-links a:focus {
444 background-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
445 background-color: {$colors['secondary_textcolor']};
448 /* Secondary Text Color */
452 .main-navigation .menu-item-description,
453 .post-navigation .meta-nav,
454 .post-navigation a:hover .post-title,
455 .post-navigation a:focus .post-title,
459 .comment-navigation a,
464 .taxonomy-description,
465 .page-links > .page-links-title,
470 .pingback .edit-link,
471 .pingback .edit-link a,
472 .post-password-form label,
475 .comment-awaiting-moderation,
483 .comment-list .reply a,
485 .widecolumn .mu_register label {
486 color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
487 color: {$colors['secondary_textcolor']};
490 /* Secondary Text Color */
492 .logged-in-as a:hover,
493 .comment-author a:hover {
494 border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
495 border-color: {$colors['secondary_textcolor']};
500 .dropdown-toggle:hover,
501 .dropdown-toggle:focus {
502 background-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
503 background-color: {$colors['border_color']};
517 .post-navigation div + div,
521 .widget_categories .children,
522 .widget_nav_menu .sub-menu,
523 .widget_pages .children,
528 .entry-content .page-links a,
532 .comment-list + .comment-respond,
533 .comment-list article,
534 .comment-list .pingback,
535 .comment-list .trackback,
536 .comment-list .reply a,
538 border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
539 border-color: {$colors['border_color']};
542 /* Border Focus Color */
546 outline-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
547 outline-color: {$colors['border_focus_color']};
552 border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
553 border-color: {$colors['border_focus_color']};
556 /* Sidebar Link Color */
557 .secondary-toggle:before {
558 color: {$colors['sidebar_textcolor']};
563 color: {$colors['sidebar_textcolor']};
566 /* Sidebar Text Color */
568 .site-title a:focus {
569 color: {$colors['secondary_sidebar_textcolor']};
572 /* Sidebar Border Color */
574 border-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */
575 border-color: {$colors['sidebar_border_color']};
578 /* Sidebar Border Focus Color */
579 .secondary-toggle:hover,
580 .secondary-toggle:focus {
581 border-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */
582 border-color: {$colors['sidebar_border_focus_color']};
586 outline-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */
587 outline-color: {$colors['sidebar_border_focus_color']};
590 /* Meta Background Color */
592 background-color: {$colors['meta_box_background_color']};
595 @media screen and (min-width: 38.75em) {
596 /* Main Text Color */
598 border-color: {$colors['textcolor']};
602 @media screen and (min-width: 59.6875em) {
603 /* Make sure its transparent on desktop */
606 background-color: transparent;
609 /* Sidebar Background Color */
611 .widget input[type="button"],
612 .widget input[type="reset"],
613 .widget input[type="submit"],
614 .widget_calendar tbody a,
615 .widget_calendar tbody a:hover,
616 .widget_calendar tbody a:focus {
617 color: {$colors['header_background_color']};
620 /* Sidebar Link Color */
622 .dropdown-toggle:after,
624 .widget blockquote cite,
625 .widget blockquote small {
626 color: {$colors['sidebar_textcolor']};
630 .widget input[type="button"],
631 .widget input[type="reset"],
632 .widget input[type="submit"],
633 .widget_calendar tbody a {
634 background-color: {$colors['sidebar_textcolor']};
638 border-color: {$colors['sidebar_textcolor']};
641 /* Sidebar Text Color */
644 .main-navigation .menu-item-description,
647 .widget .wp-caption-text,
648 .widget .gallery-caption {
649 color: {$colors['secondary_sidebar_textcolor']};
652 .widget button:hover,
653 .widget button:focus,
654 .widget input[type="button"]:hover,
655 .widget input[type="button"]:focus,
656 .widget input[type="reset"]:hover,
657 .widget input[type="reset"]:focus,
658 .widget input[type="submit"]:hover,
659 .widget input[type="submit"]:focus,
660 .widget_calendar tbody a:hover,
661 .widget_calendar tbody a:focus {
662 background-color: {$colors['secondary_sidebar_textcolor']};
666 border-color: {$colors['secondary_sidebar_textcolor']};
669 /* Sidebar Border Color */
679 .widget_categories .children,
680 .widget_nav_menu .sub-menu,
681 .widget_pages .children,
682 .widget abbr[title] {
683 border-color: {$colors['sidebar_border_color']};
686 .dropdown-toggle:hover,
687 .dropdown-toggle:focus,
689 background-color: {$colors['sidebar_border_color']};
693 .widget textarea:focus {
694 border-color: {$colors['sidebar_border_focus_color']};
698 .dropdown-toggle:focus {
699 outline-color: {$colors['sidebar_border_focus_color']};
708 * Output an Underscore template for generating CSS for the color scheme.
710 * The template generates the css dynamically for instant display in the Customizer
713 * @since Twenty Fifteen 1.0
715 function twentyfifteen_color_scheme_css_template() {
717 'background_color' => '{{ data.background_color }}',
718 'header_background_color' => '{{ data.header_background_color }}',
719 'box_background_color' => '{{ data.box_background_color }}',
720 'textcolor' => '{{ data.textcolor }}',
721 'secondary_textcolor' => '{{ data.secondary_textcolor }}',
722 'border_color' => '{{ data.border_color }}',
723 'border_focus_color' => '{{ data.border_focus_color }}',
724 'sidebar_textcolor' => '{{ data.sidebar_textcolor }}',
725 'sidebar_border_color' => '{{ data.sidebar_border_color }}',
726 'sidebar_border_focus_color' => '{{ data.sidebar_border_focus_color }}',
727 'secondary_sidebar_textcolor' => '{{ data.secondary_sidebar_textcolor }}',
728 'meta_box_background_color' => '{{ data.meta_box_background_color }}',
731 <script type="text/html" id="tmpl-twentyfifteen-color-scheme">
732 <?php echo twentyfifteen_get_color_scheme_css( $colors ); ?>
736 add_action( 'customize_controls_print_footer_scripts', 'twentyfifteen_color_scheme_css_template' );