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 if ( isset( $wp_customize->selective_refresh ) ) {
24 $wp_customize->selective_refresh->add_partial( 'blogname', array(
25 'selector' => '.site-title a',
26 'container_inclusive' => false,
27 'render_callback' => 'twentyfifteen_customize_partial_blogname',
29 $wp_customize->selective_refresh->add_partial( 'blogdescription', array(
30 'selector' => '.site-description',
31 'container_inclusive' => false,
32 'render_callback' => 'twentyfifteen_customize_partial_blogdescription',
36 // Add color scheme setting and control.
37 $wp_customize->add_setting( 'color_scheme', array(
38 'default' => 'default',
39 'sanitize_callback' => 'twentyfifteen_sanitize_color_scheme',
40 'transport' => 'postMessage',
43 $wp_customize->add_control( 'color_scheme', array(
44 'label' => __( 'Base Color Scheme', 'twentyfifteen' ),
45 'section' => 'colors',
47 'choices' => twentyfifteen_get_color_scheme_choices(),
51 // Add custom header and sidebar text color setting and control.
52 $wp_customize->add_setting( 'sidebar_textcolor', array(
53 'default' => $color_scheme[4],
54 'sanitize_callback' => 'sanitize_hex_color',
55 'transport' => 'postMessage',
58 $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'sidebar_textcolor', array(
59 'label' => __( 'Header and Sidebar Text Color', 'twentyfifteen' ),
60 'description' => __( 'Applied to the header on small screens and the sidebar on wide screens.', 'twentyfifteen' ),
61 'section' => 'colors',
64 // Remove the core header textcolor control, as it shares the sidebar text color.
65 $wp_customize->remove_control( 'header_textcolor' );
67 // Add custom header and sidebar background color setting and control.
68 $wp_customize->add_setting( 'header_background_color', array(
69 'default' => $color_scheme[1],
70 'sanitize_callback' => 'sanitize_hex_color',
71 'transport' => 'postMessage',
74 $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_background_color', array(
75 'label' => __( 'Header and Sidebar Background Color', 'twentyfifteen' ),
76 'description' => __( 'Applied to the header on small screens and the sidebar on wide screens.', 'twentyfifteen' ),
77 'section' => 'colors',
80 // Add an additional description to the header image section.
81 $wp_customize->get_section( 'header_image' )->description = __( 'Applied to the header on small screens and the sidebar on wide screens.', 'twentyfifteen' );
83 add_action( 'customize_register', 'twentyfifteen_customize_register', 11 );
86 * Render the site title for the selective refresh partial.
88 * @since Twenty Fifteen 1.5
89 * @see twentyfifteen_customize_register()
93 function twentyfifteen_customize_partial_blogname() {
98 * Render the site tagline for the selective refresh partial.
100 * @since Twenty Fifteen 1.5
101 * @see twentyfifteen_customize_register()
105 function twentyfifteen_customize_partial_blogdescription() {
106 bloginfo( 'description' );
110 * Register color schemes for Twenty Fifteen.
112 * Can be filtered with {@see 'twentyfifteen_color_schemes'}.
114 * The order of colors in a colors array:
115 * 1. Main Background Color.
116 * 2. Sidebar Background Color.
117 * 3. Box Background Color.
118 * 4. Main Text and Link Color.
119 * 5. Sidebar Text and Link Color.
120 * 6. Meta Box Background Color.
122 * @since Twenty Fifteen 1.0
124 * @return array An associative array of color scheme options.
126 function twentyfifteen_get_color_schemes() {
128 * Filter the color schemes registered for use with Twenty Fifteen.
130 * The default schemes include 'default', 'dark', 'yellow', 'pink', 'purple', and 'blue'.
132 * @since Twenty Fifteen 1.0
134 * @param array $schemes {
135 * Associative array of color schemes data.
137 * @type array $slug {
138 * Associative array of information for setting up the color scheme.
140 * @type string $label Color scheme label.
141 * @type array $colors HEX codes for default colors prepended with a hash symbol ('#').
142 * Colors are defined in the following order: Main background, sidebar
143 * background, box background, main text and link, sidebar text and link,
144 * meta box background.
148 return apply_filters( 'twentyfifteen_color_schemes', array(
150 'label' => __( 'Default', 'twentyfifteen' ),
161 'label' => __( 'Dark', 'twentyfifteen' ),
172 'label' => __( 'Yellow', 'twentyfifteen' ),
183 'label' => __( 'Pink', 'twentyfifteen' ),
194 'label' => __( 'Purple', 'twentyfifteen' ),
205 'label' => __( 'Blue', 'twentyfifteen' ),
218 if ( ! function_exists( 'twentyfifteen_get_color_scheme' ) ) :
220 * Get the current Twenty Fifteen color scheme.
222 * @since Twenty Fifteen 1.0
224 * @return array An associative array of either the current or default color scheme hex values.
226 function twentyfifteen_get_color_scheme() {
227 $color_scheme_option = get_theme_mod( 'color_scheme', 'default' );
228 $color_schemes = twentyfifteen_get_color_schemes();
230 if ( array_key_exists( $color_scheme_option, $color_schemes ) ) {
231 return $color_schemes[ $color_scheme_option ]['colors'];
234 return $color_schemes['default']['colors'];
236 endif; // twentyfifteen_get_color_scheme
238 if ( ! function_exists( 'twentyfifteen_get_color_scheme_choices' ) ) :
240 * Returns an array of color scheme choices registered for Twenty Fifteen.
242 * @since Twenty Fifteen 1.0
244 * @return array Array of color schemes.
246 function twentyfifteen_get_color_scheme_choices() {
247 $color_schemes = twentyfifteen_get_color_schemes();
248 $color_scheme_control_options = array();
250 foreach ( $color_schemes as $color_scheme => $value ) {
251 $color_scheme_control_options[ $color_scheme ] = $value['label'];
254 return $color_scheme_control_options;
256 endif; // twentyfifteen_get_color_scheme_choices
258 if ( ! function_exists( 'twentyfifteen_sanitize_color_scheme' ) ) :
260 * Sanitization callback for color schemes.
262 * @since Twenty Fifteen 1.0
264 * @param string $value Color scheme name value.
265 * @return string Color scheme name.
267 function twentyfifteen_sanitize_color_scheme( $value ) {
268 $color_schemes = twentyfifteen_get_color_scheme_choices();
270 if ( ! array_key_exists( $value, $color_schemes ) ) {
276 endif; // twentyfifteen_sanitize_color_scheme
279 * Enqueues front-end CSS for color scheme.
281 * @since Twenty Fifteen 1.0
283 * @see wp_add_inline_style()
285 function twentyfifteen_color_scheme_css() {
286 $color_scheme_option = get_theme_mod( 'color_scheme', 'default' );
288 // Don't do anything if the default color scheme is selected.
289 if ( 'default' === $color_scheme_option ) {
293 $color_scheme = twentyfifteen_get_color_scheme();
295 // Convert main and sidebar text hex color to rgba.
296 $color_textcolor_rgb = twentyfifteen_hex2rgb( $color_scheme[3] );
297 $color_sidebar_textcolor_rgb = twentyfifteen_hex2rgb( $color_scheme[4] );
299 'background_color' => $color_scheme[0],
300 'header_background_color' => $color_scheme[1],
301 'box_background_color' => $color_scheme[2],
302 'textcolor' => $color_scheme[3],
303 'secondary_textcolor' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.7)', $color_textcolor_rgb ),
304 'border_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.1)', $color_textcolor_rgb ),
305 'border_focus_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.3)', $color_textcolor_rgb ),
306 'sidebar_textcolor' => $color_scheme[4],
307 'sidebar_border_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.1)', $color_sidebar_textcolor_rgb ),
308 'sidebar_border_focus_color' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.3)', $color_sidebar_textcolor_rgb ),
309 'secondary_sidebar_textcolor' => vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.7)', $color_sidebar_textcolor_rgb ),
310 'meta_box_background_color' => $color_scheme[5],
313 $color_scheme_css = twentyfifteen_get_color_scheme_css( $colors );
315 wp_add_inline_style( 'twentyfifteen-style', $color_scheme_css );
317 add_action( 'wp_enqueue_scripts', 'twentyfifteen_color_scheme_css' );
320 * Binds JS listener to make Customizer color_scheme control.
322 * Passes color scheme data as colorScheme global.
324 * @since Twenty Fifteen 1.0
326 function twentyfifteen_customize_control_js() {
327 wp_enqueue_script( 'color-scheme-control', get_template_directory_uri() . '/js/color-scheme-control.js', array( 'customize-controls', 'iris', 'underscore', 'wp-util' ), '20141216', true );
328 wp_localize_script( 'color-scheme-control', 'colorScheme', twentyfifteen_get_color_schemes() );
330 add_action( 'customize_controls_enqueue_scripts', 'twentyfifteen_customize_control_js' );
333 * Binds JS handlers to make the Customizer preview reload changes asynchronously.
335 * @since Twenty Fifteen 1.0
337 function twentyfifteen_customize_preview_js() {
338 wp_enqueue_script( 'twentyfifteen-customize-preview', get_template_directory_uri() . '/js/customize-preview.js', array( 'customize-preview' ), '20141216', true );
340 add_action( 'customize_preview_init', 'twentyfifteen_customize_preview_js' );
343 * Returns CSS for the color schemes.
345 * @since Twenty Fifteen 1.0
347 * @param array $colors Color scheme colors.
348 * @return string Color scheme CSS.
350 function twentyfifteen_get_color_scheme_css( $colors ) {
351 $colors = wp_parse_args( $colors, array(
352 'background_color' => '',
353 'header_background_color' => '',
354 'box_background_color' => '',
356 'secondary_textcolor' => '',
357 'border_color' => '',
358 'border_focus_color' => '',
359 'sidebar_textcolor' => '',
360 'sidebar_border_color' => '',
361 'sidebar_border_focus_color' => '',
362 'secondary_sidebar_textcolor' => '',
363 'meta_box_background_color' => '',
369 /* Background Color */
371 background-color: {$colors['background_color']};
374 /* Sidebar Background Color */
377 background-color: {$colors['header_background_color']};
380 /* Box Background Color */
390 background-color: {$colors['box_background_color']};
393 /* Box Background Color */
395 input[type="button"],
397 input[type="submit"],
400 .widget_calendar tbody a,
401 .widget_calendar tbody a:hover,
402 .widget_calendar tbody a:focus,
407 color: {$colors['box_background_color']};
410 /* Main Text Color */
412 input[type="button"],
414 input[type="submit"],
417 .widget_calendar tbody a,
420 background-color: {$colors['textcolor']};
423 /* Main Text Color */
428 .dropdown-toggle:after,
429 .image-navigation a:hover,
430 .image-navigation a:focus,
431 .comment-navigation a:hover,
432 .comment-navigation a:focus,
434 .entry-footer a:hover,
435 .entry-footer a:focus,
436 .comment-metadata a:hover,
437 .comment-metadata a:focus,
438 .pingback .edit-link a:hover,
439 .pingback .edit-link a:focus,
440 .comment-list .reply a:hover,
441 .comment-list .reply a:focus,
444 color: {$colors['textcolor']};
447 /* Main Text Color */
452 .pingback .comment-body > a,
453 .author-description a,
454 .taxonomy-description a,
456 .entry-footer a:hover,
457 .comment-metadata a:hover,
458 .pingback .edit-link a:hover,
459 .comment-list .reply a:hover,
461 border-color: {$colors['textcolor']};
464 /* Secondary Text Color */
467 input[type="button"]:hover,
468 input[type="button"]:focus,
469 input[type="reset"]:hover,
470 input[type="reset"]:focus,
471 input[type="submit"]:hover,
472 input[type="submit"]:focus,
473 .pagination .prev:hover,
474 .pagination .prev:focus,
475 .pagination .next:hover,
476 .pagination .next:focus,
477 .widget_calendar tbody a:hover,
478 .widget_calendar tbody a:focus,
480 .page-links a:focus {
481 background-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
482 background-color: {$colors['secondary_textcolor']};
485 /* Secondary Text Color */
489 .main-navigation .menu-item-description,
490 .post-navigation .meta-nav,
491 .post-navigation a:hover .post-title,
492 .post-navigation a:focus .post-title,
496 .comment-navigation a,
501 .taxonomy-description,
502 .page-links > .page-links-title,
507 .pingback .edit-link,
508 .pingback .edit-link a,
509 .post-password-form label,
512 .comment-awaiting-moderation,
520 .comment-list .reply a,
522 .widecolumn .mu_register label {
523 color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
524 color: {$colors['secondary_textcolor']};
527 /* Secondary Text Color */
529 .logged-in-as a:hover,
530 .comment-author a:hover {
531 border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
532 border-color: {$colors['secondary_textcolor']};
537 .dropdown-toggle:hover,
538 .dropdown-toggle:focus {
539 background-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
540 background-color: {$colors['border_color']};
554 .post-navigation div + div,
558 .widget_categories .children,
559 .widget_nav_menu .sub-menu,
560 .widget_pages .children,
565 .entry-content .page-links a,
569 .comment-list + .comment-respond,
570 .comment-list article,
571 .comment-list .pingback,
572 .comment-list .trackback,
573 .comment-list .reply a,
575 border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
576 border-color: {$colors['border_color']};
579 /* Border Focus Color */
583 outline-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
584 outline-color: {$colors['border_focus_color']};
589 border-color: {$colors['textcolor']}; /* Fallback for IE7 and IE8 */
590 border-color: {$colors['border_focus_color']};
593 /* Sidebar Link Color */
594 .secondary-toggle:before {
595 color: {$colors['sidebar_textcolor']};
600 color: {$colors['sidebar_textcolor']};
603 /* Sidebar Text Color */
605 .site-title a:focus {
606 color: {$colors['secondary_sidebar_textcolor']};
609 /* Sidebar Border Color */
611 border-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */
612 border-color: {$colors['sidebar_border_color']};
615 /* Sidebar Border Focus Color */
616 .secondary-toggle:hover,
617 .secondary-toggle:focus {
618 border-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */
619 border-color: {$colors['sidebar_border_focus_color']};
623 outline-color: {$colors['sidebar_textcolor']}; /* Fallback for IE7 and IE8 */
624 outline-color: {$colors['sidebar_border_focus_color']};
627 /* Meta Background Color */
629 background-color: {$colors['meta_box_background_color']};
632 @media screen and (min-width: 38.75em) {
633 /* Main Text Color */
635 border-color: {$colors['textcolor']};
639 @media screen and (min-width: 59.6875em) {
640 /* Make sure its transparent on desktop */
643 background-color: transparent;
646 /* Sidebar Background Color */
648 .widget input[type="button"],
649 .widget input[type="reset"],
650 .widget input[type="submit"],
651 .widget_calendar tbody a,
652 .widget_calendar tbody a:hover,
653 .widget_calendar tbody a:focus {
654 color: {$colors['header_background_color']};
657 /* Sidebar Link Color */
659 .dropdown-toggle:after,
661 .widget blockquote cite,
662 .widget blockquote small {
663 color: {$colors['sidebar_textcolor']};
667 .widget input[type="button"],
668 .widget input[type="reset"],
669 .widget input[type="submit"],
670 .widget_calendar tbody a {
671 background-color: {$colors['sidebar_textcolor']};
675 border-color: {$colors['sidebar_textcolor']};
678 /* Sidebar Text Color */
681 .main-navigation .menu-item-description,
684 .widget .wp-caption-text,
685 .widget .gallery-caption {
686 color: {$colors['secondary_sidebar_textcolor']};
689 .widget button:hover,
690 .widget button:focus,
691 .widget input[type="button"]:hover,
692 .widget input[type="button"]:focus,
693 .widget input[type="reset"]:hover,
694 .widget input[type="reset"]:focus,
695 .widget input[type="submit"]:hover,
696 .widget input[type="submit"]:focus,
697 .widget_calendar tbody a:hover,
698 .widget_calendar tbody a:focus {
699 background-color: {$colors['secondary_sidebar_textcolor']};
703 border-color: {$colors['secondary_sidebar_textcolor']};
706 /* Sidebar Border Color */
716 .widget_categories .children,
717 .widget_nav_menu .sub-menu,
718 .widget_pages .children,
719 .widget abbr[title] {
720 border-color: {$colors['sidebar_border_color']};
723 .dropdown-toggle:hover,
724 .dropdown-toggle:focus,
726 background-color: {$colors['sidebar_border_color']};
730 .widget textarea:focus {
731 border-color: {$colors['sidebar_border_focus_color']};
735 .dropdown-toggle:focus {
736 outline-color: {$colors['sidebar_border_focus_color']};
745 * Output an Underscore template for generating CSS for the color scheme.
747 * The template generates the css dynamically for instant display in the Customizer
750 * @since Twenty Fifteen 1.0
752 function twentyfifteen_color_scheme_css_template() {
754 'background_color' => '{{ data.background_color }}',
755 'header_background_color' => '{{ data.header_background_color }}',
756 'box_background_color' => '{{ data.box_background_color }}',
757 'textcolor' => '{{ data.textcolor }}',
758 'secondary_textcolor' => '{{ data.secondary_textcolor }}',
759 'border_color' => '{{ data.border_color }}',
760 'border_focus_color' => '{{ data.border_focus_color }}',
761 'sidebar_textcolor' => '{{ data.sidebar_textcolor }}',
762 'sidebar_border_color' => '{{ data.sidebar_border_color }}',
763 'sidebar_border_focus_color' => '{{ data.sidebar_border_focus_color }}',
764 'secondary_sidebar_textcolor' => '{{ data.secondary_sidebar_textcolor }}',
765 'meta_box_background_color' => '{{ data.meta_box_background_color }}',
768 <script type="text/html" id="tmpl-twentyfifteen-color-scheme">
769 <?php echo twentyfifteen_get_color_scheme_css( $colors ); ?>
773 add_action( 'customize_controls_print_footer_scripts', 'twentyfifteen_color_scheme_css_template' );