functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /**
  3. * Twenty Sixteen functions and definitions
  4. *
  5. * Set up the theme and provides some helper functions, which are used in the
  6. * theme as custom template tags. Others are attached to action and filter
  7. * hooks in WordPress to change core functionality.
  8. *
  9. * When using a child theme you can override certain functions (those wrapped
  10. * in a function_exists() call) by defining them first in your child theme's
  11. * functions.php file. The child theme's functions.php file is included before
  12. * the parent theme's file, so the child theme functions would be used.
  13. *
  14. * @link https://codex.wordpress.org/Theme_Development
  15. * @link https://codex.wordpress.org/Child_Themes
  16. *
  17. * Functions that are not pluggable (not wrapped in function_exists()) are
  18. * instead attached to a filter or action hook.
  19. *
  20. * For more information on hooks, actions, and filters,
  21. * {@link https://codex.wordpress.org/Plugin_API}
  22. *
  23. * @package WordPress
  24. * @subpackage Twenty_Sixteen
  25. * @since Twenty Sixteen 1.0
  26. */
  27. /**
  28. * Twenty Sixteen only works in WordPress 4.4 or later.
  29. */
  30. if ( version_compare( $GLOBALS['wp_version'], '4.4-alpha', '<' ) ) {
  31. require get_template_directory() . '/inc/back-compat.php';
  32. }
  33. if ( ! function_exists( 'twentysixteen_setup' ) ) :
  34. /**
  35. * Sets up theme defaults and registers support for various WordPress features.
  36. *
  37. * Note that this function is hooked into the after_setup_theme hook, which
  38. * runs before the init hook. The init hook is too late for some features, such
  39. * as indicating support for post thumbnails.
  40. *
  41. * Create your own twentysixteen_setup() function to override in a child theme.
  42. *
  43. * @since Twenty Sixteen 1.0
  44. */
  45. function twentysixteen_setup() {
  46. /*
  47. * Make theme available for translation.
  48. * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/twentysixteen
  49. * If you're building a theme based on Twenty Sixteen, use a find and replace
  50. * to change 'twentysixteen' to the name of your theme in all the template files
  51. */
  52. load_theme_textdomain( 'twentysixteen' );
  53. // Add default posts and comments RSS feed links to head.
  54. add_theme_support( 'automatic-feed-links' );
  55. /*
  56. * Let WordPress manage the document title.
  57. * By adding theme support, we declare that this theme does not use a
  58. * hard-coded <title> tag in the document head, and expect WordPress to
  59. * provide it for us.
  60. */
  61. add_theme_support( 'title-tag' );
  62. /*
  63. * Enable support for custom logo.
  64. *
  65. * @since Twenty Sixteen 1.2
  66. */
  67. add_theme_support( 'custom-logo', array(
  68. 'height' => 240,
  69. 'width' => 240,
  70. 'flex-height' => true,
  71. ) );
  72. /*
  73. * Enable support for Post Thumbnails on posts and pages.
  74. *
  75. * @link https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
  76. */
  77. add_theme_support( 'post-thumbnails' );
  78. set_post_thumbnail_size( 1200, 9999 );
  79. // This theme uses wp_nav_menu() in two locations.
  80. register_nav_menus( array(
  81. 'primary' => __( 'Primary Menu', 'twentysixteen' ),
  82. 'social' => __( 'Social Links Menu', 'twentysixteen' ),
  83. ) );
  84. /*
  85. * Switch default core markup for search form, comment form, and comments
  86. * to output valid HTML5.
  87. */
  88. add_theme_support( 'html5', array(
  89. 'search-form',
  90. 'comment-form',
  91. 'comment-list',
  92. 'gallery',
  93. 'caption',
  94. ) );
  95. /*
  96. * Enable support for Post Formats.
  97. *
  98. * See: https://codex.wordpress.org/Post_Formats
  99. */
  100. add_theme_support( 'post-formats', array(
  101. 'aside',
  102. 'image',
  103. 'video',
  104. 'quote',
  105. 'link',
  106. 'gallery',
  107. 'status',
  108. 'audio',
  109. 'chat',
  110. ) );
  111. /*
  112. * This theme styles the visual editor to resemble the theme style,
  113. * specifically font, colors, icons, and column width.
  114. */
  115. add_editor_style( array( 'css/editor-style.css', twentysixteen_fonts_url() ) );
  116. // Indicate widget sidebars can use selective refresh in the Customizer.
  117. add_theme_support( 'customize-selective-refresh-widgets' );
  118. }
  119. endif; // twentysixteen_setup
  120. add_action( 'after_setup_theme', 'twentysixteen_setup' );
  121. /**
  122. * Sets the content width in pixels, based on the theme's design and stylesheet.
  123. *
  124. * Priority 0 to make it available to lower priority callbacks.
  125. *
  126. * @global int $content_width
  127. *
  128. * @since Twenty Sixteen 1.0
  129. */
  130. function twentysixteen_content_width() {
  131. $GLOBALS['content_width'] = apply_filters( 'twentysixteen_content_width', 840 );
  132. }
  133. add_action( 'after_setup_theme', 'twentysixteen_content_width', 0 );
  134. /**
  135. * Registers a widget area.
  136. *
  137. * @link https://developer.wordpress.org/reference/functions/register_sidebar/
  138. *
  139. * @since Twenty Sixteen 1.0
  140. */
  141. function twentysixteen_widgets_init() {
  142. register_sidebar( array(
  143. 'name' => __( 'Sidebar', 'twentysixteen' ),
  144. 'id' => 'sidebar-1',
  145. 'description' => __( 'Add widgets here to appear in your sidebar.', 'twentysixteen' ),
  146. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  147. 'after_widget' => '</section>',
  148. 'before_title' => '<h2 class="widget-title">',
  149. 'after_title' => '</h2>',
  150. ) );
  151. register_sidebar( array(
  152. 'name' => __( 'Content Bottom 1', 'twentysixteen' ),
  153. 'id' => 'sidebar-2',
  154. 'description' => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ),
  155. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  156. 'after_widget' => '</section>',
  157. 'before_title' => '<h2 class="widget-title">',
  158. 'after_title' => '</h2>',
  159. ) );
  160. register_sidebar( array(
  161. 'name' => __( 'Content Bottom 2', 'twentysixteen' ),
  162. 'id' => 'sidebar-3',
  163. 'description' => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ),
  164. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  165. 'after_widget' => '</section>',
  166. 'before_title' => '<h2 class="widget-title">',
  167. 'after_title' => '</h2>',
  168. ) );
  169. }
  170. add_action( 'widgets_init', 'twentysixteen_widgets_init' );
  171. if ( ! function_exists( 'twentysixteen_fonts_url' ) ) :
  172. /**
  173. * Register Google fonts for Twenty Sixteen.
  174. *
  175. * Create your own twentysixteen_fonts_url() function to override in a child theme.
  176. *
  177. * @since Twenty Sixteen 1.0
  178. *
  179. * @return string Google fonts URL for the theme.
  180. */
  181. function twentysixteen_fonts_url() {
  182. $fonts_url = '';
  183. $fonts = array();
  184. $subsets = 'latin,latin-ext';
  185. /* translators: If there are characters in your language that are not supported by Merriweather, translate this to 'off'. Do not translate into your own language. */
  186. if ( 'off' !== _x( 'on', 'Merriweather font: on or off', 'twentysixteen' ) ) {
  187. $fonts[] = 'Merriweather:400,700,900,400italic,700italic,900italic';
  188. }
  189. /* translators: If there are characters in your language that are not supported by Montserrat, translate this to 'off'. Do not translate into your own language. */
  190. if ( 'off' !== _x( 'on', 'Montserrat font: on or off', 'twentysixteen' ) ) {
  191. $fonts[] = 'Montserrat:400,700';
  192. }
  193. /* translators: If there are characters in your language that are not supported by Inconsolata, translate this to 'off'. Do not translate into your own language. */
  194. if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentysixteen' ) ) {
  195. $fonts[] = 'Inconsolata:400';
  196. }
  197. if ( $fonts ) {
  198. $fonts_url = add_query_arg( array(
  199. 'family' => urlencode( implode( '|', $fonts ) ),
  200. 'subset' => urlencode( $subsets ),
  201. ), 'https://fonts.googleapis.com/css' );
  202. }
  203. return $fonts_url;
  204. }
  205. endif;
  206. /**
  207. * Handles JavaScript detection.
  208. *
  209. * Adds a `js` class to the root `<html>` element when JavaScript is detected.
  210. *
  211. * @since Twenty Sixteen 1.0
  212. */
  213. function twentysixteen_javascript_detection() {
  214. echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
  215. }
  216. add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 );
  217. /**
  218. * Enqueues scripts and styles.
  219. *
  220. * @since Twenty Sixteen 1.0
  221. */
  222. function twentysixteen_scripts() {
  223. // Add custom fonts, used in the main stylesheet.
  224. wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), null );
  225. // Add Genericons, used in the main stylesheet.
  226. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.4.1' );
  227. // Theme stylesheet.
  228. wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri() );
  229. // Load the Internet Explorer specific stylesheet.
  230. wp_enqueue_style( 'twentysixteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentysixteen-style' ), '20160816' );
  231. wp_style_add_data( 'twentysixteen-ie', 'conditional', 'lt IE 10' );
  232. // Load the Internet Explorer 8 specific stylesheet.
  233. wp_enqueue_style( 'twentysixteen-ie8', get_template_directory_uri() . '/css/ie8.css', array( 'twentysixteen-style' ), '20160816' );
  234. wp_style_add_data( 'twentysixteen-ie8', 'conditional', 'lt IE 9' );
  235. // Load the Internet Explorer 7 specific stylesheet.
  236. wp_enqueue_style( 'twentysixteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentysixteen-style' ), '20160816' );
  237. wp_style_add_data( 'twentysixteen-ie7', 'conditional', 'lt IE 8' );
  238. // Load the html5 shiv.
  239. wp_enqueue_script( 'twentysixteen-html5', get_template_directory_uri() . '/js/html5.js', array(), '3.7.3' );
  240. wp_script_add_data( 'twentysixteen-html5', 'conditional', 'lt IE 9' );
  241. wp_enqueue_script( 'twentysixteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20160816', true );
  242. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  243. wp_enqueue_script( 'comment-reply' );
  244. }
  245. if ( is_singular() && wp_attachment_is_image() ) {
  246. wp_enqueue_script( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20160816' );
  247. }
  248. wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20160816', true );
  249. wp_localize_script( 'twentysixteen-script', 'screenReaderText', array(
  250. 'expand' => __( 'expand child menu', 'twentysixteen' ),
  251. 'collapse' => __( 'collapse child menu', 'twentysixteen' ),
  252. ) );
  253. }
  254. add_action( 'wp_enqueue_scripts', 'twentysixteen_scripts' );
  255. /**
  256. * Adds custom classes to the array of body classes.
  257. *
  258. * @since Twenty Sixteen 1.0
  259. *
  260. * @param array $classes Classes for the body element.
  261. * @return array (Maybe) filtered body classes.
  262. */
  263. function twentysixteen_body_classes( $classes ) {
  264. // Adds a class of custom-background-image to sites with a custom background image.
  265. if ( get_background_image() ) {
  266. $classes[] = 'custom-background-image';
  267. }
  268. // Adds a class of group-blog to sites with more than 1 published author.
  269. if ( is_multi_author() ) {
  270. $classes[] = 'group-blog';
  271. }
  272. // Adds a class of no-sidebar to sites without active sidebar.
  273. if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  274. $classes[] = 'no-sidebar';
  275. }
  276. // Adds a class of hfeed to non-singular pages.
  277. if ( ! is_singular() ) {
  278. $classes[] = 'hfeed';
  279. }
  280. return $classes;
  281. }
  282. add_filter( 'body_class', 'twentysixteen_body_classes' );
  283. /**
  284. * Converts a HEX value to RGB.
  285. *
  286. * @since Twenty Sixteen 1.0
  287. *
  288. * @param string $color The original color, in 3- or 6-digit hexadecimal form.
  289. * @return array Array containing RGB (red, green, and blue) values for the given
  290. * HEX code, empty array otherwise.
  291. */
  292. function twentysixteen_hex2rgb( $color ) {
  293. $color = trim( $color, '#' );
  294. if ( strlen( $color ) === 3 ) {
  295. $r = hexdec( substr( $color, 0, 1 ).substr( $color, 0, 1 ) );
  296. $g = hexdec( substr( $color, 1, 1 ).substr( $color, 1, 1 ) );
  297. $b = hexdec( substr( $color, 2, 1 ).substr( $color, 2, 1 ) );
  298. } else if ( strlen( $color ) === 6 ) {
  299. $r = hexdec( substr( $color, 0, 2 ) );
  300. $g = hexdec( substr( $color, 2, 2 ) );
  301. $b = hexdec( substr( $color, 4, 2 ) );
  302. } else {
  303. return array();
  304. }
  305. return array( 'red' => $r, 'green' => $g, 'blue' => $b );
  306. }
  307. /**
  308. * Custom template tags for this theme.
  309. */
  310. require get_template_directory() . '/inc/template-tags.php';
  311. /**
  312. * Customizer additions.
  313. */
  314. require get_template_directory() . '/inc/customizer.php';
  315. /**
  316. * Add custom image sizes attribute to enhance responsive image functionality
  317. * for content images
  318. *
  319. * @since Twenty Sixteen 1.0
  320. *
  321. * @param string $sizes A source size value for use in a 'sizes' attribute.
  322. * @param array $size Image size. Accepts an array of width and height
  323. * values in pixels (in that order).
  324. * @return string A source size value for use in a content image 'sizes' attribute.
  325. */
  326. function twentysixteen_content_image_sizes_attr( $sizes, $size ) {
  327. $width = $size[0];
  328. if ( 840 <= $width ) {
  329. $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px';
  330. }
  331. if ( 'page' === get_post_type() ) {
  332. if ( 840 > $width ) {
  333. $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
  334. }
  335. } else {
  336. if ( 840 > $width && 600 <= $width ) {
  337. $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px';
  338. } elseif ( 600 > $width ) {
  339. $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
  340. }
  341. }
  342. return $sizes;
  343. }
  344. add_filter( 'wp_calculate_image_sizes', 'twentysixteen_content_image_sizes_attr', 10 , 2 );
  345. /**
  346. * Add custom image sizes attribute to enhance responsive image functionality
  347. * for post thumbnails
  348. *
  349. * @since Twenty Sixteen 1.0
  350. *
  351. * @param array $attr Attributes for the image markup.
  352. * @param int $attachment Image attachment ID.
  353. * @param array $size Registered image size or flat array of height and width dimensions.
  354. * @return array The filtered attributes for the image markup.
  355. */
  356. function twentysixteen_post_thumbnail_sizes_attr( $attr, $attachment, $size ) {
  357. if ( 'post-thumbnail' === $size ) {
  358. if ( is_active_sidebar( 'sidebar-1' ) ) {
  359. $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 60vw, (max-width: 1362px) 62vw, 840px';
  360. } else {
  361. $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 88vw, 1200px';
  362. }
  363. }
  364. return $attr;
  365. }
  366. add_filter( 'wp_get_attachment_image_attributes', 'twentysixteen_post_thumbnail_sizes_attr', 10 , 3 );
  367. /**
  368. * Modifies tag cloud widget arguments to display all tags in the same font size
  369. * and use list format for better accessibility.
  370. *
  371. * @since Twenty Sixteen 1.1
  372. *
  373. * @param array $args Arguments for tag cloud widget.
  374. * @return array The filtered arguments for tag cloud widget.
  375. */
  376. function twentysixteen_widget_tag_cloud_args( $args ) {
  377. $args['largest'] = 1;
  378. $args['smallest'] = 1;
  379. $args['unit'] = 'em';
  380. $args['format'] = 'list';
  381. return $args;
  382. }
  383. add_filter( 'widget_tag_cloud_args', 'twentysixteen_widget_tag_cloud_args' );