functions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 in the /languages/ directory.
  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', get_template_directory() . '/languages' );
  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 Post Thumbnails on posts and pages.
  64. *
  65. * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
  66. */
  67. add_theme_support( 'post-thumbnails' );
  68. set_post_thumbnail_size( 1200, 0, true );
  69. // This theme uses wp_nav_menu() in two locations.
  70. register_nav_menus( array(
  71. 'primary' => __( 'Primary Menu', 'twentysixteen' ),
  72. 'social' => __( 'Social Links Menu', 'twentysixteen' ),
  73. ) );
  74. /*
  75. * Switch default core markup for search form, comment form, and comments
  76. * to output valid HTML5.
  77. */
  78. add_theme_support( 'html5', array(
  79. 'search-form',
  80. 'comment-form',
  81. 'comment-list',
  82. 'gallery',
  83. 'caption',
  84. ) );
  85. /*
  86. * Enable support for Post Formats.
  87. *
  88. * See: https://codex.wordpress.org/Post_Formats
  89. */
  90. add_theme_support( 'post-formats', array(
  91. 'aside',
  92. 'image',
  93. 'video',
  94. 'quote',
  95. 'link',
  96. 'gallery',
  97. 'status',
  98. 'audio',
  99. 'chat',
  100. ) );
  101. /*
  102. * This theme styles the visual editor to resemble the theme style,
  103. * specifically font, colors, icons, and column width.
  104. */
  105. add_editor_style( array( 'css/editor-style.css', twentysixteen_fonts_url() ) );
  106. }
  107. endif; // twentysixteen_setup
  108. add_action( 'after_setup_theme', 'twentysixteen_setup' );
  109. /**
  110. * Sets the content width in pixels, based on the theme's design and stylesheet.
  111. *
  112. * Priority 0 to make it available to lower priority callbacks.
  113. *
  114. * @global int $content_width
  115. *
  116. * @since Twenty Sixteen 1.0
  117. */
  118. function twentysixteen_content_width() {
  119. $GLOBALS['content_width'] = apply_filters( 'twentysixteen_content_width', 840 );
  120. }
  121. add_action( 'after_setup_theme', 'twentysixteen_content_width', 0 );
  122. /**
  123. * Registers a widget area.
  124. *
  125. * @link https://developer.wordpress.org/reference/functions/register_sidebar/
  126. *
  127. * @since Twenty Sixteen 1.0
  128. */
  129. function twentysixteen_widgets_init() {
  130. register_sidebar( array(
  131. 'name' => __( 'Sidebar', 'twentysixteen' ),
  132. 'id' => 'sidebar-1',
  133. 'description' => __( 'Add widgets here to appear in your sidebar.', 'twentysixteen' ),
  134. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  135. 'after_widget' => '</aside>',
  136. 'before_title' => '<h2 class="widget-title">',
  137. 'after_title' => '</h2>',
  138. ) );
  139. register_sidebar( array(
  140. 'name' => __( 'Content Bottom 1', 'twentysixteen' ),
  141. 'id' => 'sidebar-2',
  142. 'description' => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ),
  143. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  144. 'after_widget' => '</aside>',
  145. 'before_title' => '<h2 class="widget-title">',
  146. 'after_title' => '</h2>',
  147. ) );
  148. register_sidebar( array(
  149. 'name' => __( 'Content Bottom 2', 'twentysixteen' ),
  150. 'id' => 'sidebar-3',
  151. 'description' => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ),
  152. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  153. 'after_widget' => '</aside>',
  154. 'before_title' => '<h2 class="widget-title">',
  155. 'after_title' => '</h2>',
  156. ) );
  157. }
  158. add_action( 'widgets_init', 'twentysixteen_widgets_init' );
  159. if ( ! function_exists( 'twentysixteen_fonts_url' ) ) :
  160. /**
  161. * Register Google fonts for Twenty Sixteen.
  162. *
  163. * Create your own twentysixteen_fonts_url() function to override in a child theme.
  164. *
  165. * @since Twenty Sixteen 1.0
  166. *
  167. * @return string Google fonts URL for the theme.
  168. */
  169. function twentysixteen_fonts_url() {
  170. $fonts_url = '';
  171. $fonts = array();
  172. $subsets = 'latin,latin-ext';
  173. /* 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. */
  174. if ( 'off' !== _x( 'on', 'Merriweather font: on or off', 'twentysixteen' ) ) {
  175. $fonts[] = 'Merriweather:400,700,900,400italic,700italic,900italic';
  176. }
  177. /* 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. */
  178. if ( 'off' !== _x( 'on', 'Montserrat font: on or off', 'twentysixteen' ) ) {
  179. $fonts[] = 'Montserrat:400,700';
  180. }
  181. /* 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. */
  182. if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentysixteen' ) ) {
  183. $fonts[] = 'Inconsolata:400';
  184. }
  185. if ( $fonts ) {
  186. $fonts_url = add_query_arg( array(
  187. 'family' => urlencode( implode( '|', $fonts ) ),
  188. 'subset' => urlencode( $subsets ),
  189. ), 'https://fonts.googleapis.com/css' );
  190. }
  191. return $fonts_url;
  192. }
  193. endif;
  194. /**
  195. * Handles JavaScript detection.
  196. *
  197. * Adds a `js` class to the root `<html>` element when JavaScript is detected.
  198. *
  199. * @since Twenty Sixteen 1.0
  200. */
  201. function twentysixteen_javascript_detection() {
  202. echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
  203. }
  204. add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 );
  205. /**
  206. * Enqueues scripts and styles.
  207. *
  208. * @since Twenty Sixteen 1.0
  209. */
  210. function twentysixteen_scripts() {
  211. // Add custom fonts, used in the main stylesheet.
  212. wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), null );
  213. // Add Genericons, used in the main stylesheet.
  214. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.4.1' );
  215. // Theme stylesheet.
  216. wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri() );
  217. // Load the Internet Explorer specific stylesheet.
  218. wp_enqueue_style( 'twentysixteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentysixteen-style' ), '20151005' );
  219. wp_style_add_data( 'twentysixteen-ie', 'conditional', 'lt IE 10' );
  220. // Load the Internet Explorer 8 specific stylesheet.
  221. wp_enqueue_style( 'twentysixteen-ie8', get_template_directory_uri() . '/css/ie8.css', array( 'twentysixteen-style' ), '20151005' );
  222. wp_style_add_data( 'twentysixteen-ie8', 'conditional', 'lt IE 9' );
  223. // Load the Internet Explorer 7 specific stylesheet.
  224. wp_enqueue_style( 'twentysixteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentysixteen-style' ), '20151005' );
  225. wp_style_add_data( 'twentysixteen-ie7', 'conditional', 'lt IE 8' );
  226. // Load the html5 shiv.
  227. wp_enqueue_script( 'twentysixteen-html5', get_template_directory_uri() . '/js/html5.js', array(), '3.7.3' );
  228. wp_script_add_data( 'twentysixteen-html5', 'conditional', 'lt IE 9' );
  229. wp_enqueue_script( 'twentysixteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151005', true );
  230. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  231. wp_enqueue_script( 'comment-reply' );
  232. }
  233. if ( is_singular() && wp_attachment_is_image() ) {
  234. wp_enqueue_script( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20151005' );
  235. }
  236. wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20151005', true );
  237. wp_localize_script( 'twentysixteen-script', 'screenReaderText', array(
  238. 'expand' => __( 'expand child menu', 'twentysixteen' ),
  239. 'collapse' => __( 'collapse child menu', 'twentysixteen' ),
  240. ) );
  241. }
  242. add_action( 'wp_enqueue_scripts', 'twentysixteen_scripts' );
  243. /**
  244. * Adds custom classes to the array of body classes.
  245. *
  246. * @since Twenty Sixteen 1.0
  247. *
  248. * @param array $classes Classes for the body element.
  249. * @return array (Maybe) filtered body classes.
  250. */
  251. function twentysixteen_body_classes( $classes ) {
  252. // Adds a class of custom-background-image to sites with a custom background image.
  253. if ( get_background_image() ) {
  254. $classes[] = 'custom-background-image';
  255. }
  256. // Adds a class of group-blog to sites with more than 1 published author.
  257. if ( is_multi_author() ) {
  258. $classes[] = 'group-blog';
  259. }
  260. // Adds a class of no-sidebar to sites without active sidebar.
  261. if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  262. $classes[] = 'no-sidebar';
  263. }
  264. return $classes;
  265. }
  266. add_filter( 'body_class', 'twentysixteen_body_classes' );
  267. /**
  268. * Converts a HEX value to RGB.
  269. *
  270. * @since Twenty Sixteen 1.0
  271. *
  272. * @param string $color The original color, in 3- or 6-digit hexadecimal form.
  273. * @return array Array containing RGB (red, green, and blue) values for the given
  274. * HEX code, empty array otherwise.
  275. */
  276. function twentysixteen_hex2rgb( $color ) {
  277. $color = trim( $color, '#' );
  278. if ( strlen( $color ) == 3 ) {
  279. $r = hexdec( substr( $color, 0, 1 ).substr( $color, 0, 1 ) );
  280. $g = hexdec( substr( $color, 1, 1 ).substr( $color, 1, 1 ) );
  281. $b = hexdec( substr( $color, 2, 1 ).substr( $color, 2, 1 ) );
  282. } else if ( strlen( $color ) == 6 ) {
  283. $r = hexdec( substr( $color, 0, 2 ) );
  284. $g = hexdec( substr( $color, 2, 2 ) );
  285. $b = hexdec( substr( $color, 4, 2 ) );
  286. } else {
  287. return array();
  288. }
  289. return array( 'red' => $r, 'green' => $g, 'blue' => $b );
  290. }
  291. /**
  292. * Custom template tags for this theme.
  293. */
  294. require get_template_directory() . '/inc/template-tags.php';
  295. /**
  296. * Customizer additions.
  297. */
  298. require get_template_directory() . '/inc/customizer.php';