custom-header.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * Implement Custom Header functionality for Twenty Fifteen.
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Fifteen
  7. * @since Twenty Fifteen 1.0
  8. */
  9. /**
  10. * Set up the WordPress core custom header feature.
  11. *
  12. * @uses twentyfifteen_header_style()
  13. * @uses twentyfifteen_admin_header_image()
  14. */
  15. function twentyfifteen_custom_header_setup() {
  16. $color_scheme = twentyfifteen_get_color_scheme();
  17. $default_text_color = trim( $color_scheme[4], '#' );
  18. /**
  19. * Filter Twenty Fifteen custom-header support arguments.
  20. *
  21. * @since Twenty Fifteen 1.0
  22. *
  23. * @param array $args {
  24. * An array of custom-header support arguments.
  25. *
  26. * @type string $default_text_color Default color of the header text.
  27. * @type int $width Width in pixels of the custom header image. Default 954.
  28. * @type int $height Height in pixels of the custom header image. Default 1300.
  29. * @type string $wp-head-callback Callback function used to styles the header image and text
  30. * displayed on the blog.
  31. * @type string $admin-preview-callback Callback function used to create the custom header markup in
  32. * the Appearance > Header screen.
  33. * }
  34. */
  35. add_theme_support( 'custom-header', apply_filters( 'twentyfifteen_custom_header_args', array(
  36. 'default-text-color' => $default_text_color,
  37. 'width' => 954,
  38. 'height' => 1300,
  39. 'wp-head-callback' => 'twentyfifteen_header_style',
  40. 'admin-preview-callback' => 'twentyfifteen_admin_header_image',
  41. ) ) );
  42. }
  43. add_action( 'after_setup_theme', 'twentyfifteen_custom_header_setup' );
  44. /**
  45. * Convert HEX to RGB.
  46. *
  47. * @since Twenty Fifteen 1.0
  48. *
  49. * @param string $color The original color, in 3- or 6-digit hexadecimal form.
  50. * @return array
  51. */
  52. function twentyfifteen_hex2rgb( $color ) {
  53. $color = trim( $color, '#' );
  54. if ( strlen( $color ) == 3 ) {
  55. $r = hexdec( substr( $color, 0, 1 ).substr( $color, 0, 1 ) );
  56. $g = hexdec( substr( $color, 1, 1 ).substr( $color, 1, 1 ) );
  57. $b = hexdec( substr( $color, 2, 1 ).substr( $color, 2, 1 ) );
  58. } else if ( strlen( $color ) == 6 ) {
  59. $r = hexdec( substr( $color, 0, 2 ) );
  60. $g = hexdec( substr( $color, 2, 2 ) );
  61. $b = hexdec( substr( $color, 4, 2 ) );
  62. } else {
  63. return array();
  64. }
  65. return array( 'red' => $r, 'green' => $g, 'blue' => $b );
  66. }
  67. if ( ! function_exists( 'twentyfifteen_header_style' ) ) :
  68. /**
  69. * Styles the header image and text displayed on the blog.
  70. *
  71. * @since Twenty Fifteen 1.0
  72. * @see twentyfifteen_custom_header_setup().
  73. */
  74. function twentyfifteen_header_style() {
  75. $header_image = get_header_image();
  76. $text_color = get_header_textcolor();
  77. // If no custom options for text are set, let's bail.
  78. if ( empty( $header_image ) && $text_color == get_theme_support( 'custom-header', 'default-text-color' ) ) {
  79. return;
  80. }
  81. // If we get this far, we have custom styles. Let's do this.
  82. ?>
  83. <style type="text/css" id="twentyfifteen-header-css">
  84. <?php
  85. if ( ! empty( $header_image ) ) :
  86. ?>
  87. .site-header {
  88. background: url(<?php header_image(); ?>) no-repeat 50% 50%;
  89. -webkit-background-size: cover;
  90. -moz-background-size: cover;
  91. -o-background-size: cover;
  92. background-size: cover;
  93. }
  94. @media screen and (min-width: 59.6875em) {
  95. body:before {
  96. background: url(<?php header_image(); ?>) no-repeat 100% 50%;
  97. -webkit-background-size: cover;
  98. -moz-background-size: cover;
  99. -o-background-size: cover;
  100. background-size: cover;
  101. border-right: 0;
  102. }
  103. .site-header {
  104. background: transparent;
  105. }
  106. }
  107. <?php
  108. endif;
  109. // Has the text been hidden?
  110. if ( ! display_header_text() ) :
  111. ?>
  112. .site-title,
  113. .site-description {
  114. clip: rect(1px, 1px, 1px, 1px);
  115. position: absolute;
  116. }
  117. <?php
  118. // If the user has set a custom color for the text use that
  119. elseif ( $text_color != get_theme_support( 'custom-header', 'default-text-color' ) ) :
  120. ?>
  121. .site-title a,
  122. .site-title a:hover,
  123. .site-title a:focus,
  124. .site-description {
  125. color: #<?php echo esc_attr( $text_color ); ?>;
  126. }
  127. <?php endif; ?>
  128. </style>
  129. <?php
  130. }
  131. endif; // twentyfifteen_header_style
  132. if ( ! function_exists( 'twentyfifteen_admin_header_image' ) ) :
  133. /**
  134. * Custom header image markup displayed on the Appearance > Header admin panel.
  135. *
  136. * @since Twenty Fifteen 1.0
  137. * @see twentyfifteen_custom_header_setup().
  138. */
  139. function twentyfifteen_admin_header_image() {
  140. $style = sprintf( ' style="color:#%s;"', get_header_textcolor() );
  141. $color_scheme = twentyfifteen_get_color_scheme();
  142. $header_background_color = get_theme_mod( 'header_background_color', $color_scheme[1] );
  143. ?>
  144. <div id="headimg" style="background-image: url(<?php header_image(); ?>); background-color: <?php echo esc_attr( $header_background_color ); ?>;">
  145. <h1 class="displaying-header-text"><a id="name"<?php echo $style; ?> onclick="return false;" href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
  146. <div id="desc" class="displaying-header-text"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
  147. </div>
  148. <?php
  149. }
  150. endif; // twentyfifteen_admin_header_image
  151. /**
  152. * Enqueues front-end CSS for the header background color.
  153. *
  154. * @since Twenty Fifteen 1.0
  155. */
  156. function twentyfifteen_header_background_color_css() {
  157. $header_background_color = get_theme_mod( 'header_background_color', '#ffffff' );
  158. // Don't do anything if the current color is the default.
  159. if ( '#ffffff' === $header_background_color ) {
  160. return;
  161. }
  162. $css = '
  163. /* Custom Header Background Color */
  164. body:before,
  165. .site-header {
  166. background-color: %1$s;
  167. }
  168. @media screen and (min-width: 59.6875em) {
  169. .site-header {
  170. background-color: transparent;
  171. }
  172. .widget button,
  173. .widget input[type="button"],
  174. .widget input[type="reset"],
  175. .widget input[type="submit"],
  176. .widget_calendar tbody a,
  177. .widget_calendar tbody a:hover,
  178. .widget_calendar tbody a:focus {
  179. color: %1$s;
  180. }
  181. }
  182. ';
  183. wp_add_inline_style( 'twentyfifteen-style', sprintf( $css, $header_background_color ) );
  184. }
  185. add_action( 'wp_enqueue_scripts', 'twentyfifteen_header_background_color_css', 11 );
  186. /**
  187. * Enqueues front-end CSS for the sidebar text color.
  188. *
  189. * @since Twenty Fifteen 1.0
  190. */
  191. function twentyfifteen_sidebar_text_color_css() {
  192. $sidebar_link_color = get_theme_mod( 'sidebar_textcolor', '#333333' );
  193. // Don't do anything if the current color is the default.
  194. if ( '#333333' === $sidebar_link_color ) {
  195. return;
  196. }
  197. // If we get this far, we have custom styles. Let's do this.
  198. $sidebar_link_color_rgb = twentyfifteen_hex2rgb( $sidebar_link_color );
  199. $sidebar_text_color = vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.7)', $sidebar_link_color_rgb );
  200. $sidebar_border_color = vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.1)', $sidebar_link_color_rgb );
  201. $sidebar_border_focus_color = vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.3)', $sidebar_link_color_rgb );
  202. $css = '
  203. /* Custom Sidebar Text Color */
  204. .secondary-toggle:before {
  205. color: %1$s;
  206. }
  207. .secondary-toggle:focus {
  208. outline-color: %1$s;
  209. }
  210. .secondary-toggle:hover {
  211. border-color: %4$s;
  212. }
  213. @media screen and (min-width: 59.6875em) {
  214. .dropdown-toggle:after,
  215. .main-navigation a,
  216. .social-navigation a,
  217. .widget-title,
  218. .widget a {
  219. color: %1$s;
  220. }
  221. .dropdown-toggle:focus {
  222. outline-color: %1$s;
  223. }
  224. .widget button,
  225. .widget input[type="button"],
  226. .widget input[type="reset"],
  227. .widget input[type="submit"],
  228. .widget_calendar tbody a {
  229. background-color: %1$s;
  230. }
  231. .main-navigation .menu-item-description,
  232. .main-navigation a:hover,
  233. .main-navigation a:focus,
  234. .social-navigation a:hover:before,
  235. .social-navigation a:focus:before,
  236. .widget,
  237. .widget a:hover,
  238. .widget a:focus,
  239. .widget blockquote,
  240. .widget blockquote cite,
  241. .widget blockquote small,
  242. .widget .wp-caption-text,
  243. .widget .gallery-caption {
  244. color: %1$s; /* Fallback for IE7 and IE8 */
  245. color: %2$s;
  246. }
  247. .widget_calendar tbody a:hover,
  248. .widget_calendar tbody a:focus {
  249. background-color: %1$s; /* Fallback for IE7 and IE8 */
  250. background-color: %2$s;
  251. }
  252. .widget blockquote {
  253. border-color: %2$s;
  254. }
  255. .main-navigation ul,
  256. .main-navigation li,
  257. .secondary-toggle,
  258. .widget_categories .children,
  259. .widget_nav_menu .sub-menu,
  260. .widget_pages .children,
  261. .widget table,
  262. .widget th,
  263. .widget td,
  264. .widget input,
  265. .widget textarea,
  266. .widget pre,
  267. .widget li,
  268. .widget abbr[title] {
  269. border-color: %1$s; /* Fallback for IE7 and IE8 */
  270. border-color: %3$s;
  271. }
  272. .dropdown-toggle:hover,
  273. .dropdown-toggle:focus,
  274. .widget hr {
  275. background-color: %1$s; /* Fallback for IE7 and IE8 */
  276. background-color: %3$s;
  277. }
  278. .widget input:focus,
  279. .widget textarea:focus {
  280. border-color: %1$s; /* Fallback for IE7 and IE8 */
  281. border-color: %4$s;
  282. }
  283. }
  284. ';
  285. wp_add_inline_style( 'twentyfifteen-style', sprintf( $css, $sidebar_link_color, $sidebar_text_color, $sidebar_border_color, $sidebar_border_focus_color ) );
  286. }
  287. add_action( 'wp_enqueue_scripts', 'twentyfifteen_sidebar_text_color_css', 11 );
  288. /**
  289. * Enqueue styles to admin screen for custom header display.
  290. *
  291. * @since Twenty Fifteen 1.0
  292. */
  293. function twentyfifteen_admin_fonts() {
  294. wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
  295. wp_enqueue_style( 'twentyfifteen-custom-header', get_template_directory_uri() . '/css/admin-custom-header.css', array(), '20141010' );
  296. }
  297. add_action( 'admin_print_scripts-appearance_page_custom-header', 'twentyfifteen_admin_fonts' );