functions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 http://codex.wordpress.org/Theme_Development
  15. * @link http://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 http://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. if ( ! function_exists( 'twentyfifteen_setup' ) ) :
  36. /**
  37. * Sets up theme defaults and registers support for various WordPress features.
  38. *
  39. * Note that this function is hooked into the after_setup_theme hook, which
  40. * runs before the init hook. The init hook is too late for some features, such
  41. * as indicating support for post thumbnails.
  42. *
  43. * @since Twenty Fifteen 1.0
  44. */
  45. function twentyfifteen_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 twentyfifteen, use a find and replace
  50. * to change 'twentyfifteen' to the name of your theme in all the template files
  51. */
  52. load_theme_textdomain( 'twentyfifteen', get_template_directory() . '/languages' );
  53. // Add default posts and comments RSS feed links to head.
  54. add_theme_support( 'automatic-feed-links' );
  55. /*
  56. * Enable support for Post Thumbnails on posts and pages.
  57. *
  58. * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
  59. */
  60. add_theme_support( 'post-thumbnails' );
  61. set_post_thumbnail_size( 825, 510, true );
  62. // This theme uses wp_nav_menu() in one location.
  63. register_nav_menus( array(
  64. 'primary' => __( 'Primary Menu', 'twentyfifteen' ),
  65. 'social' => __( 'Social Links Menu', 'twentyfifteen' ),
  66. ) );
  67. /*
  68. * Switch default core markup for search form, comment form, and comments
  69. * to output valid HTML5.
  70. */
  71. add_theme_support( 'html5', array(
  72. 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
  73. ) );
  74. /*
  75. * Enable support for Post Formats.
  76. * See http://codex.wordpress.org/Post_Formats
  77. */
  78. add_theme_support( 'post-formats', array(
  79. 'aside', 'image', 'video', 'quote', 'link', 'gallery', 'status', 'audio', 'chat'
  80. ) );
  81. $color_scheme = twentyfifteen_get_color_scheme();
  82. $default_color = trim( $color_scheme[0], '#' );
  83. // Setup the WordPress core custom background feature.
  84. add_theme_support( 'custom-background', apply_filters( 'twentyfifteen_custom_background_args', array(
  85. 'default-color' => $default_color,
  86. 'default-attachment' => 'fixed',
  87. ) ) );
  88. /*
  89. * This theme styles the visual editor to resemble the theme style,
  90. * specifically font, colors, icons, and column width.
  91. */
  92. add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
  93. }
  94. endif; // twentyfifteen_setup
  95. add_action( 'after_setup_theme', 'twentyfifteen_setup' );
  96. /**
  97. * Register widget area.
  98. *
  99. * @since Twenty Fifteen 1.0
  100. *
  101. * @link http://codex.wordpress.org/Function_Reference/register_sidebar
  102. */
  103. function twentyfifteen_widgets_init() {
  104. register_sidebar( array(
  105. 'name' => __( 'Widget Area', 'twentyfifteen' ),
  106. 'id' => 'sidebar-1',
  107. 'description' => __( 'Add widgets here to appear in your sidebar.', 'twentyfifteen' ),
  108. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  109. 'after_widget' => '</aside>',
  110. 'before_title' => '<h1 class="widget-title">',
  111. 'after_title' => '</h1>',
  112. ) );
  113. }
  114. add_action( 'widgets_init', 'twentyfifteen_widgets_init' );
  115. if ( ! function_exists( 'twentyfifteen_fonts_url' ) ) :
  116. /**
  117. * Register Google fonts for Twenty Fifteen.
  118. *
  119. * @since Twenty Fifteen 1.0
  120. *
  121. * @return string
  122. */
  123. function twentyfifteen_fonts_url() {
  124. $fonts = array();
  125. $subsets = 'latin,latin-ext';
  126. /*
  127. * Translators: If there are characters in your language that are not supported
  128. * by Noto Sans, translate this to 'off'. Do not translate into your own language.
  129. */
  130. if ( 'off' !== _x( 'on', 'Noto Sans font: on or off', 'twentyfifteen' ) ) {
  131. $fonts[] = 'Noto Sans:400italic,700italic,400,700';
  132. }
  133. /*
  134. * Translators: If there are characters in your language that are not supported
  135. * by Noto Serif, translate this to 'off'. Do not translate into your own language.
  136. */
  137. if ( 'off' !== _x( 'on', 'Noto Serif font: on or off', 'twentyfifteen' ) ) {
  138. $fonts[] = 'Noto Serif:400italic,700italic,400,700';
  139. }
  140. /*
  141. * Translators: If there are characters in your language that are not supported
  142. * by Inconsolata, translate this to 'off'. Do not translate into your own language.
  143. */
  144. if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentyfifteen' ) ) {
  145. $fonts[] = 'Inconsolata:400,700';
  146. }
  147. /*
  148. * Translators: To add an additional character subset specific to your language,
  149. * translate this to 'greek', 'cyrillic', 'devanagari' or 'vietnamese'. Do not translate into your own language.
  150. */
  151. $subset = _x( 'no-subset', 'Add new subset (greek, cyrillic, devanagari, vietnamese)', 'twentyfifteen' );
  152. if ( 'cyrillic' == $subset ) {
  153. $subsets .= ',cyrillic,cyrillic-ext';
  154. } elseif ( 'greek' == $subset ) {
  155. $subsets .= ',greek,greek-ext';
  156. } elseif ( 'devanagari' == $subset ) {
  157. $subsets .= ',devanagari';
  158. } elseif ( 'vietnamese' == $subset ) {
  159. $subsets .= ',vietnamese';
  160. }
  161. return add_query_arg( array(
  162. 'family' => urlencode( implode( '|', $fonts ) ),
  163. 'subset' => urlencode( $subsets ),
  164. ), '//fonts.googleapis.com/css' );
  165. }
  166. endif;
  167. /**
  168. * Enqueue scripts and styles.
  169. *
  170. * @since Twenty Fifteen 1.0
  171. */
  172. function twentyfifteen_scripts() {
  173. // Add custom fonts, used in the main stylesheet.
  174. wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
  175. // Add Genericons, used in the main stylesheet.
  176. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
  177. // Load our main stylesheet.
  178. wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );
  179. // Load the Internet Explorer specific stylesheet.
  180. wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style', 'genericons' ), '20141010' );
  181. wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );
  182. // Load the Internet Explorer 7 specific stylesheet.
  183. wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
  184. wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
  185. wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );
  186. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  187. wp_enqueue_script( 'comment-reply' );
  188. }
  189. if ( is_singular() && wp_attachment_is_image() ) {
  190. wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
  191. }
  192. wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20141010', true );
  193. wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
  194. 'expand' => '<span class="screen-reader-text">' . esc_html__( 'Expand', 'twentyfifteen' ) . '</span>',
  195. 'collapse' => '<span class="screen-reader-text">' . esc_html__( 'Collapse', 'twentyfifteen' ) . '</span>',
  196. ) );
  197. }
  198. add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
  199. /**
  200. * Filters wp_title to print a neat <title> tag based on what is being viewed.
  201. *
  202. * @param string $title Default title text for current view.
  203. * @param string $sep Optional separator.
  204. * @return string The filtered title.
  205. */
  206. function twentyfifteen_wp_title( $title, $sep ) {
  207. if ( is_feed() ) {
  208. return $title;
  209. }
  210. global $page, $paged;
  211. // Add the blog name
  212. $title .= get_bloginfo( 'name', 'display' );
  213. // Add the blog description for the home/front page.
  214. $site_description = get_bloginfo( 'description', 'display' );
  215. if ( $site_description && ( is_home() || is_front_page() ) ) {
  216. $title .= " $sep $site_description";
  217. }
  218. // Add a page number if necessary:
  219. if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
  220. $title .= " $sep " . sprintf( __( 'Page %s', 'twentyfifteen' ), max( $paged, $page ) );
  221. }
  222. return $title;
  223. }
  224. add_filter( 'wp_title', 'twentyfifteen_wp_title', 10, 2 );
  225. /**
  226. * Add featured image as background image to post navs.
  227. *
  228. * @since Twenty Fifteen 1.0
  229. */
  230. function twentyfifteen_post_nav_background() {
  231. if ( ! is_single() ) {
  232. return;
  233. }
  234. $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
  235. $next = get_adjacent_post( false, '', false );
  236. $css = '';
  237. if ( is_attachment() && 'attachment' == $previous->post_type ) {
  238. return;
  239. }
  240. if ( $previous && has_post_thumbnail( $previous->ID ) ) {
  241. $prevthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'post-thumbnail' );
  242. $css .= '.post-navigation .nav-previous { background-image: url(' . esc_url( $prevthumb[0] ) . '); }';
  243. }
  244. if ( $next && has_post_thumbnail( $next->ID ) ) {
  245. $nextthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'post-thumbnail' );
  246. $css .= '.post-navigation .nav-next { background-image: url(' . esc_url( $nextthumb[0] ) . '); }';
  247. }
  248. wp_add_inline_style( 'twentyfifteen-style', $css );
  249. }
  250. add_action( 'wp_enqueue_scripts', 'twentyfifteen_post_nav_background' );
  251. /**
  252. * Display descriptions in main navigation.
  253. *
  254. * @since Twenty Fifteen 1.0
  255. *
  256. * @param string $item_output The menu item output.
  257. * @param WP_Post $item Menu item object.
  258. * @param int $depth Depth of the menu.
  259. * @param array $args wp_nav_menu() arguments.
  260. *
  261. * @return string Menu item with possible description.
  262. */
  263. function twentyfifteen_nav_description( $item_output, $item, $depth, $args ) {
  264. if ( 'primary' == $args->theme_location && $item->description ) {
  265. $item_output = str_replace( $args->link_after . '</a>', '<span class="menu-item-description">' . $item->description . '</span>' . $args->link_after . '</a>', $item_output );
  266. }
  267. return $item_output;
  268. }
  269. add_filter( 'walker_nav_menu_start_el', 'twentyfifteen_nav_description', 10, 4 );
  270. /**
  271. * Implement the Custom Header feature.
  272. *
  273. * @since Twenty Fifteen 1.0
  274. */
  275. require get_template_directory() . '/inc/custom-header.php';
  276. /**
  277. * Custom template tags for this theme.
  278. *
  279. * @since Twenty Fifteen 1.0
  280. */
  281. require get_template_directory() . '/inc/template-tags.php';
  282. /**
  283. * Customizer additions.
  284. *
  285. * @since Twenty Fifteen 1.0
  286. */
  287. require get_template_directory() . '/inc/customizer.php';