functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * Twenty Fifteen 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_Fifteen
  25. * @since Twenty Fifteen 1.0
  26. */
  27. /**
  28. * Set the content width based on the theme's design and stylesheet.
  29. *
  30. * @since Twenty Fifteen 1.0
  31. */
  32. if ( ! isset( $content_width ) ) {
  33. $content_width = 660;
  34. }
  35. /**
  36. * Twenty Fifteen only works in WordPress 4.1 or later.
  37. */
  38. if ( version_compare( $GLOBALS['wp_version'], '4.1-alpha', '<' ) ) {
  39. require get_template_directory() . '/inc/back-compat.php';
  40. }
  41. if ( ! function_exists( 'twentyfifteen_setup' ) ) :
  42. /**
  43. * Sets up theme defaults and registers support for various WordPress features.
  44. *
  45. * Note that this function is hooked into the after_setup_theme hook, which
  46. * runs before the init hook. The init hook is too late for some features, such
  47. * as indicating support for post thumbnails.
  48. *
  49. * @since Twenty Fifteen 1.0
  50. */
  51. function twentyfifteen_setup() {
  52. /*
  53. * Make theme available for translation.
  54. * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/twentyfifteen
  55. * If you're building a theme based on twentyfifteen, use a find and replace
  56. * to change 'twentyfifteen' to the name of your theme in all the template files
  57. */
  58. load_theme_textdomain( 'twentyfifteen' );
  59. // Add default posts and comments RSS feed links to head.
  60. add_theme_support( 'automatic-feed-links' );
  61. /*
  62. * Let WordPress manage the document title.
  63. * By adding theme support, we declare that this theme does not use a
  64. * hard-coded <title> tag in the document head, and expect WordPress to
  65. * provide it for us.
  66. */
  67. add_theme_support( 'title-tag' );
  68. /*
  69. * Enable support for Post Thumbnails on posts and pages.
  70. *
  71. * See: https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
  72. */
  73. add_theme_support( 'post-thumbnails' );
  74. set_post_thumbnail_size( 825, 510, true );
  75. // This theme uses wp_nav_menu() in two locations.
  76. register_nav_menus( array(
  77. 'primary' => __( 'Primary Menu', 'twentyfifteen' ),
  78. 'social' => __( 'Social Links Menu', 'twentyfifteen' ),
  79. ) );
  80. /*
  81. * Switch default core markup for search form, comment form, and comments
  82. * to output valid HTML5.
  83. */
  84. add_theme_support( 'html5', array(
  85. 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
  86. ) );
  87. /*
  88. * Enable support for Post Formats.
  89. *
  90. * See: https://codex.wordpress.org/Post_Formats
  91. */
  92. add_theme_support( 'post-formats', array(
  93. 'aside', 'image', 'video', 'quote', 'link', 'gallery', 'status', 'audio', 'chat'
  94. ) );
  95. /*
  96. * Enable support for custom logo.
  97. *
  98. * @since Twenty Fifteen 1.5
  99. */
  100. add_theme_support( 'custom-logo', array(
  101. 'height' => 248,
  102. 'width' => 248,
  103. 'flex-height' => true,
  104. ) );
  105. $color_scheme = twentyfifteen_get_color_scheme();
  106. $default_color = trim( $color_scheme[0], '#' );
  107. // Setup the WordPress core custom background feature.
  108. /**
  109. * Filter Twenty Fifteen custom-header support arguments.
  110. *
  111. * @since Twenty Fifteen 1.0
  112. *
  113. * @param array $args {
  114. * An array of custom-header support arguments.
  115. *
  116. * @type string $default-color Default color of the header.
  117. * @type string $default-attachment Default attachment of the header.
  118. * }
  119. */
  120. add_theme_support( 'custom-background', apply_filters( 'twentyfifteen_custom_background_args', array(
  121. 'default-color' => $default_color,
  122. 'default-attachment' => 'fixed',
  123. ) ) );
  124. /*
  125. * This theme styles the visual editor to resemble the theme style,
  126. * specifically font, colors, icons, and column width.
  127. */
  128. add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
  129. // Indicate widget sidebars can use selective refresh in the Customizer.
  130. add_theme_support( 'customize-selective-refresh-widgets' );
  131. }
  132. endif; // twentyfifteen_setup
  133. add_action( 'after_setup_theme', 'twentyfifteen_setup' );
  134. /**
  135. * Register widget area.
  136. *
  137. * @since Twenty Fifteen 1.0
  138. *
  139. * @link https://codex.wordpress.org/Function_Reference/register_sidebar
  140. */
  141. function twentyfifteen_widgets_init() {
  142. register_sidebar( array(
  143. 'name' => __( 'Widget Area', 'twentyfifteen' ),
  144. 'id' => 'sidebar-1',
  145. 'description' => __( 'Add widgets here to appear in your sidebar.', 'twentyfifteen' ),
  146. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  147. 'after_widget' => '</aside>',
  148. 'before_title' => '<h2 class="widget-title">',
  149. 'after_title' => '</h2>',
  150. ) );
  151. }
  152. add_action( 'widgets_init', 'twentyfifteen_widgets_init' );
  153. if ( ! function_exists( 'twentyfifteen_fonts_url' ) ) :
  154. /**
  155. * Register Google fonts for Twenty Fifteen.
  156. *
  157. * @since Twenty Fifteen 1.0
  158. *
  159. * @return string Google fonts URL for the theme.
  160. */
  161. function twentyfifteen_fonts_url() {
  162. $fonts_url = '';
  163. $fonts = array();
  164. $subsets = 'latin,latin-ext';
  165. /*
  166. * Translators: If there are characters in your language that are not supported
  167. * by Noto Sans, translate this to 'off'. Do not translate into your own language.
  168. */
  169. if ( 'off' !== _x( 'on', 'Noto Sans font: on or off', 'twentyfifteen' ) ) {
  170. $fonts[] = 'Noto Sans:400italic,700italic,400,700';
  171. }
  172. /*
  173. * Translators: If there are characters in your language that are not supported
  174. * by Noto Serif, translate this to 'off'. Do not translate into your own language.
  175. */
  176. if ( 'off' !== _x( 'on', 'Noto Serif font: on or off', 'twentyfifteen' ) ) {
  177. $fonts[] = 'Noto Serif:400italic,700italic,400,700';
  178. }
  179. /*
  180. * Translators: If there are characters in your language that are not supported
  181. * by Inconsolata, translate this to 'off'. Do not translate into your own language.
  182. */
  183. if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentyfifteen' ) ) {
  184. $fonts[] = 'Inconsolata:400,700';
  185. }
  186. /*
  187. * Translators: To add an additional character subset specific to your language,
  188. * translate this to 'greek', 'cyrillic', 'devanagari' or 'vietnamese'. Do not translate into your own language.
  189. */
  190. $subset = _x( 'no-subset', 'Add new subset (greek, cyrillic, devanagari, vietnamese)', 'twentyfifteen' );
  191. if ( 'cyrillic' == $subset ) {
  192. $subsets .= ',cyrillic,cyrillic-ext';
  193. } elseif ( 'greek' == $subset ) {
  194. $subsets .= ',greek,greek-ext';
  195. } elseif ( 'devanagari' == $subset ) {
  196. $subsets .= ',devanagari';
  197. } elseif ( 'vietnamese' == $subset ) {
  198. $subsets .= ',vietnamese';
  199. }
  200. if ( $fonts ) {
  201. $fonts_url = add_query_arg( array(
  202. 'family' => urlencode( implode( '|', $fonts ) ),
  203. 'subset' => urlencode( $subsets ),
  204. ), 'https://fonts.googleapis.com/css' );
  205. }
  206. return $fonts_url;
  207. }
  208. endif;
  209. /**
  210. * JavaScript Detection.
  211. *
  212. * Adds a `js` class to the root `<html>` element when JavaScript is detected.
  213. *
  214. * @since Twenty Fifteen 1.1
  215. */
  216. function twentyfifteen_javascript_detection() {
  217. echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
  218. }
  219. add_action( 'wp_head', 'twentyfifteen_javascript_detection', 0 );
  220. /**
  221. * Enqueue scripts and styles.
  222. *
  223. * @since Twenty Fifteen 1.0
  224. */
  225. function twentyfifteen_scripts() {
  226. // Add custom fonts, used in the main stylesheet.
  227. wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
  228. // Add Genericons, used in the main stylesheet.
  229. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
  230. // Load our main stylesheet.
  231. wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );
  232. // Load the Internet Explorer specific stylesheet.
  233. wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20141010' );
  234. wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );
  235. // Load the Internet Explorer 7 specific stylesheet.
  236. wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
  237. wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
  238. wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );
  239. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  240. wp_enqueue_script( 'comment-reply' );
  241. }
  242. if ( is_singular() && wp_attachment_is_image() ) {
  243. wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
  244. }
  245. wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150330', true );
  246. wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
  247. 'expand' => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
  248. 'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
  249. ) );
  250. }
  251. add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
  252. /**
  253. * Add preconnect for Google Fonts.
  254. *
  255. * @since Twenty Fifteen 1.7
  256. *
  257. * @param array $urls URLs to print for resource hints.
  258. * @param string $relation_type The relation type the URLs are printed.
  259. * @return array URLs to print for resource hints.
  260. */
  261. function twentyfifteen_resource_hints( $urls, $relation_type ) {
  262. if ( wp_style_is( 'twentyfifteen-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
  263. if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '>=' ) ) {
  264. $urls[] = array(
  265. 'href' => 'https://fonts.gstatic.com',
  266. 'crossorigin',
  267. );
  268. } else {
  269. $urls[] = 'https://fonts.gstatic.com';
  270. }
  271. }
  272. return $urls;
  273. }
  274. add_filter( 'wp_resource_hints', 'twentyfifteen_resource_hints', 10, 2 );
  275. /**
  276. * Add featured image as background image to post navigation elements.
  277. *
  278. * @since Twenty Fifteen 1.0
  279. *
  280. * @see wp_add_inline_style()
  281. */
  282. function twentyfifteen_post_nav_background() {
  283. if ( ! is_single() ) {
  284. return;
  285. }
  286. $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
  287. $next = get_adjacent_post( false, '', false );
  288. $css = '';
  289. if ( is_attachment() && 'attachment' == $previous->post_type ) {
  290. return;
  291. }
  292. if ( $previous && has_post_thumbnail( $previous->ID ) ) {
  293. $prevthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'post-thumbnail' );
  294. $css .= '
  295. .post-navigation .nav-previous { background-image: url(' . esc_url( $prevthumb[0] ) . '); }
  296. .post-navigation .nav-previous .post-title, .post-navigation .nav-previous a:hover .post-title, .post-navigation .nav-previous .meta-nav { color: #fff; }
  297. .post-navigation .nav-previous a:before { background-color: rgba(0, 0, 0, 0.4); }
  298. ';
  299. }
  300. if ( $next && has_post_thumbnail( $next->ID ) ) {
  301. $nextthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'post-thumbnail' );
  302. $css .= '
  303. .post-navigation .nav-next { background-image: url(' . esc_url( $nextthumb[0] ) . '); border-top: 0; }
  304. .post-navigation .nav-next .post-title, .post-navigation .nav-next a:hover .post-title, .post-navigation .nav-next .meta-nav { color: #fff; }
  305. .post-navigation .nav-next a:before { background-color: rgba(0, 0, 0, 0.4); }
  306. ';
  307. }
  308. wp_add_inline_style( 'twentyfifteen-style', $css );
  309. }
  310. add_action( 'wp_enqueue_scripts', 'twentyfifteen_post_nav_background' );
  311. /**
  312. * Display descriptions in main navigation.
  313. *
  314. * @since Twenty Fifteen 1.0
  315. *
  316. * @param string $item_output The menu item output.
  317. * @param WP_Post $item Menu item object.
  318. * @param int $depth Depth of the menu.
  319. * @param array $args wp_nav_menu() arguments.
  320. * @return string Menu item with possible description.
  321. */
  322. function twentyfifteen_nav_description( $item_output, $item, $depth, $args ) {
  323. if ( 'primary' == $args->theme_location && $item->description ) {
  324. $item_output = str_replace( $args->link_after . '</a>', '<div class="menu-item-description">' . $item->description . '</div>' . $args->link_after . '</a>', $item_output );
  325. }
  326. return $item_output;
  327. }
  328. add_filter( 'walker_nav_menu_start_el', 'twentyfifteen_nav_description', 10, 4 );
  329. /**
  330. * Add a `screen-reader-text` class to the search form's submit button.
  331. *
  332. * @since Twenty Fifteen 1.0
  333. *
  334. * @param string $html Search form HTML.
  335. * @return string Modified search form HTML.
  336. */
  337. function twentyfifteen_search_form_modify( $html ) {
  338. return str_replace( 'class="search-submit"', 'class="search-submit screen-reader-text"', $html );
  339. }
  340. add_filter( 'get_search_form', 'twentyfifteen_search_form_modify' );
  341. /**
  342. * Implement the Custom Header feature.
  343. *
  344. * @since Twenty Fifteen 1.0
  345. */
  346. require get_template_directory() . '/inc/custom-header.php';
  347. /**
  348. * Custom template tags for this theme.
  349. *
  350. * @since Twenty Fifteen 1.0
  351. */
  352. require get_template_directory() . '/inc/template-tags.php';
  353. /**
  354. * Customizer additions.
  355. *
  356. * @since Twenty Fifteen 1.0
  357. */
  358. require get_template_directory() . '/inc/customizer.php';