functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 in the /languages/ directory.
  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', get_template_directory() . '/languages' );
  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. $color_scheme = twentyfifteen_get_color_scheme();
  96. $default_color = trim( $color_scheme[0], '#' );
  97. // Setup the WordPress core custom background feature.
  98. add_theme_support( 'custom-background', apply_filters( 'twentyfifteen_custom_background_args', array(
  99. 'default-color' => $default_color,
  100. 'default-attachment' => 'fixed',
  101. ) ) );
  102. /*
  103. * This theme styles the visual editor to resemble the theme style,
  104. * specifically font, colors, icons, and column width.
  105. */
  106. add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
  107. }
  108. endif; // twentyfifteen_setup
  109. add_action( 'after_setup_theme', 'twentyfifteen_setup' );
  110. /**
  111. * Register widget area.
  112. *
  113. * @since Twenty Fifteen 1.0
  114. *
  115. * @link https://codex.wordpress.org/Function_Reference/register_sidebar
  116. */
  117. function twentyfifteen_widgets_init() {
  118. register_sidebar( array(
  119. 'name' => __( 'Widget Area', 'twentyfifteen' ),
  120. 'id' => 'sidebar-1',
  121. 'description' => __( 'Add widgets here to appear in your sidebar.', 'twentyfifteen' ),
  122. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  123. 'after_widget' => '</aside>',
  124. 'before_title' => '<h2 class="widget-title">',
  125. 'after_title' => '</h2>',
  126. ) );
  127. }
  128. add_action( 'widgets_init', 'twentyfifteen_widgets_init' );
  129. if ( ! function_exists( 'twentyfifteen_fonts_url' ) ) :
  130. /**
  131. * Register Google fonts for Twenty Fifteen.
  132. *
  133. * @since Twenty Fifteen 1.0
  134. *
  135. * @return string Google fonts URL for the theme.
  136. */
  137. function twentyfifteen_fonts_url() {
  138. $fonts_url = '';
  139. $fonts = array();
  140. $subsets = 'latin,latin-ext';
  141. /*
  142. * Translators: If there are characters in your language that are not supported
  143. * by Noto Sans, translate this to 'off'. Do not translate into your own language.
  144. */
  145. if ( 'off' !== _x( 'on', 'Noto Sans font: on or off', 'twentyfifteen' ) ) {
  146. $fonts[] = 'Noto Sans:400italic,700italic,400,700';
  147. }
  148. /*
  149. * Translators: If there are characters in your language that are not supported
  150. * by Noto Serif, translate this to 'off'. Do not translate into your own language.
  151. */
  152. if ( 'off' !== _x( 'on', 'Noto Serif font: on or off', 'twentyfifteen' ) ) {
  153. $fonts[] = 'Noto Serif:400italic,700italic,400,700';
  154. }
  155. /*
  156. * Translators: If there are characters in your language that are not supported
  157. * by Inconsolata, translate this to 'off'. Do not translate into your own language.
  158. */
  159. if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentyfifteen' ) ) {
  160. $fonts[] = 'Inconsolata:400,700';
  161. }
  162. /*
  163. * Translators: To add an additional character subset specific to your language,
  164. * translate this to 'greek', 'cyrillic', 'devanagari' or 'vietnamese'. Do not translate into your own language.
  165. */
  166. $subset = _x( 'no-subset', 'Add new subset (greek, cyrillic, devanagari, vietnamese)', 'twentyfifteen' );
  167. if ( 'cyrillic' == $subset ) {
  168. $subsets .= ',cyrillic,cyrillic-ext';
  169. } elseif ( 'greek' == $subset ) {
  170. $subsets .= ',greek,greek-ext';
  171. } elseif ( 'devanagari' == $subset ) {
  172. $subsets .= ',devanagari';
  173. } elseif ( 'vietnamese' == $subset ) {
  174. $subsets .= ',vietnamese';
  175. }
  176. if ( $fonts ) {
  177. $fonts_url = add_query_arg( array(
  178. 'family' => urlencode( implode( '|', $fonts ) ),
  179. 'subset' => urlencode( $subsets ),
  180. ), '//fonts.googleapis.com/css' );
  181. }
  182. return $fonts_url;
  183. }
  184. endif;
  185. /**
  186. * JavaScript Detection.
  187. *
  188. * Adds a `js` class to the root `<html>` element when JavaScript is detected.
  189. *
  190. * @since Twenty Fifteen 1.1
  191. */
  192. function twentyfifteen_javascript_detection() {
  193. echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
  194. }
  195. add_action( 'wp_head', 'twentyfifteen_javascript_detection', 0 );
  196. /**
  197. * Enqueue scripts and styles.
  198. *
  199. * @since Twenty Fifteen 1.0
  200. */
  201. function twentyfifteen_scripts() {
  202. // Add custom fonts, used in the main stylesheet.
  203. wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
  204. // Add Genericons, used in the main stylesheet.
  205. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
  206. // Load our main stylesheet.
  207. wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );
  208. // Load the Internet Explorer specific stylesheet.
  209. wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20141010' );
  210. wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );
  211. // Load the Internet Explorer 7 specific stylesheet.
  212. wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
  213. wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
  214. wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );
  215. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  216. wp_enqueue_script( 'comment-reply' );
  217. }
  218. if ( is_singular() && wp_attachment_is_image() ) {
  219. wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
  220. }
  221. wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150330', true );
  222. wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
  223. 'expand' => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
  224. 'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
  225. ) );
  226. }
  227. add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
  228. /**
  229. * Add featured image as background image to post navigation elements.
  230. *
  231. * @since Twenty Fifteen 1.0
  232. *
  233. * @see wp_add_inline_style()
  234. */
  235. function twentyfifteen_post_nav_background() {
  236. if ( ! is_single() ) {
  237. return;
  238. }
  239. $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
  240. $next = get_adjacent_post( false, '', false );
  241. $css = '';
  242. if ( is_attachment() && 'attachment' == $previous->post_type ) {
  243. return;
  244. }
  245. if ( $previous && has_post_thumbnail( $previous->ID ) ) {
  246. $prevthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'post-thumbnail' );
  247. $css .= '
  248. .post-navigation .nav-previous { background-image: url(' . esc_url( $prevthumb[0] ) . '); }
  249. .post-navigation .nav-previous .post-title, .post-navigation .nav-previous a:hover .post-title, .post-navigation .nav-previous .meta-nav { color: #fff; }
  250. .post-navigation .nav-previous a:before { background-color: rgba(0, 0, 0, 0.4); }
  251. ';
  252. }
  253. if ( $next && has_post_thumbnail( $next->ID ) ) {
  254. $nextthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'post-thumbnail' );
  255. $css .= '
  256. .post-navigation .nav-next { background-image: url(' . esc_url( $nextthumb[0] ) . '); border-top: 0; }
  257. .post-navigation .nav-next .post-title, .post-navigation .nav-next a:hover .post-title, .post-navigation .nav-next .meta-nav { color: #fff; }
  258. .post-navigation .nav-next a:before { background-color: rgba(0, 0, 0, 0.4); }
  259. ';
  260. }
  261. wp_add_inline_style( 'twentyfifteen-style', $css );
  262. }
  263. add_action( 'wp_enqueue_scripts', 'twentyfifteen_post_nav_background' );
  264. /**
  265. * Display descriptions in main navigation.
  266. *
  267. * @since Twenty Fifteen 1.0
  268. *
  269. * @param string $item_output The menu item output.
  270. * @param WP_Post $item Menu item object.
  271. * @param int $depth Depth of the menu.
  272. * @param array $args wp_nav_menu() arguments.
  273. * @return string Menu item with possible description.
  274. */
  275. function twentyfifteen_nav_description( $item_output, $item, $depth, $args ) {
  276. if ( 'primary' == $args->theme_location && $item->description ) {
  277. $item_output = str_replace( $args->link_after . '</a>', '<div class="menu-item-description">' . $item->description . '</div>' . $args->link_after . '</a>', $item_output );
  278. }
  279. return $item_output;
  280. }
  281. add_filter( 'walker_nav_menu_start_el', 'twentyfifteen_nav_description', 10, 4 );
  282. /**
  283. * Add a `screen-reader-text` class to the search form's submit button.
  284. *
  285. * @since Twenty Fifteen 1.0
  286. *
  287. * @param string $html Search form HTML.
  288. * @return string Modified search form HTML.
  289. */
  290. function twentyfifteen_search_form_modify( $html ) {
  291. return str_replace( 'class="search-submit"', 'class="search-submit screen-reader-text"', $html );
  292. }
  293. add_filter( 'get_search_form', 'twentyfifteen_search_form_modify' );
  294. /**
  295. * Implement the Custom Header feature.
  296. *
  297. * @since Twenty Fifteen 1.0
  298. */
  299. require get_template_directory() . '/inc/custom-header.php';
  300. /**
  301. * Custom template tags for this theme.
  302. *
  303. * @since Twenty Fifteen 1.0
  304. */
  305. require get_template_directory() . '/inc/template-tags.php';
  306. /**
  307. * Customizer additions.
  308. *
  309. * @since Twenty Fifteen 1.0
  310. */
  311. require get_template_directory() . '/inc/customizer.php';