functions.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php
  2. /**
  3. * Twenty Fourteen 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_Fourteen
  25. * @since Twenty Fourteen 1.0
  26. */
  27. /**
  28. * Set up the content width value based on the theme's design.
  29. *
  30. * @see twentyfourteen_content_width()
  31. *
  32. * @since Twenty Fourteen 1.0
  33. */
  34. if ( ! isset( $content_width ) ) {
  35. $content_width = 474;
  36. }
  37. /**
  38. * Twenty Fourteen only works in WordPress 3.6 or later.
  39. */
  40. if ( version_compare( $GLOBALS['wp_version'], '3.6', '<' ) ) {
  41. require get_template_directory() . '/inc/back-compat.php';
  42. }
  43. if ( ! function_exists( 'twentyfourteen_setup' ) ) :
  44. /**
  45. * Twenty Fourteen setup.
  46. *
  47. * Set up theme defaults and registers support for various WordPress features.
  48. *
  49. * Note that this function is hooked into the after_setup_theme hook, which
  50. * runs before the init hook. The init hook is too late for some features, such
  51. * as indicating support post thumbnails.
  52. *
  53. * @since Twenty Fourteen 1.0
  54. */
  55. function twentyfourteen_setup() {
  56. /*
  57. * Make Twenty Fourteen available for translation.
  58. *
  59. * Translations can be added to the /languages/ directory.
  60. * If you're building a theme based on Twenty Fourteen, use a find and
  61. * replace to change 'twentyfourteen' to the name of your theme in all
  62. * template files.
  63. */
  64. load_theme_textdomain( 'twentyfourteen', get_template_directory() . '/languages' );
  65. // This theme styles the visual editor to resemble the theme style.
  66. add_editor_style( array( 'css/editor-style.css', twentyfourteen_font_url(), 'genericons/genericons.css' ) );
  67. // Add RSS feed links to <head> for posts and comments.
  68. add_theme_support( 'automatic-feed-links' );
  69. // Enable support for Post Thumbnails, and declare two sizes.
  70. add_theme_support( 'post-thumbnails' );
  71. set_post_thumbnail_size( 672, 372, true );
  72. add_image_size( 'twentyfourteen-full-width', 1038, 576, true );
  73. // This theme uses wp_nav_menu() in two locations.
  74. register_nav_menus( array(
  75. 'primary' => __( 'Top primary menu', 'twentyfourteen' ),
  76. 'secondary' => __( 'Secondary menu in left sidebar', 'twentyfourteen' ),
  77. ) );
  78. /*
  79. * Switch default core markup for search form, comment form, and comments
  80. * to output valid HTML5.
  81. */
  82. add_theme_support( 'html5', array(
  83. 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
  84. ) );
  85. /*
  86. * Enable support for Post Formats.
  87. * See https://codex.wordpress.org/Post_Formats
  88. */
  89. add_theme_support( 'post-formats', array(
  90. 'aside', 'image', 'video', 'audio', 'quote', 'link', 'gallery',
  91. ) );
  92. // This theme allows users to set a custom background.
  93. add_theme_support( 'custom-background', apply_filters( 'twentyfourteen_custom_background_args', array(
  94. 'default-color' => 'f5f5f5',
  95. ) ) );
  96. // Add support for featured content.
  97. add_theme_support( 'featured-content', array(
  98. 'featured_content_filter' => 'twentyfourteen_get_featured_posts',
  99. 'max_posts' => 6,
  100. ) );
  101. // This theme uses its own gallery styles.
  102. add_filter( 'use_default_gallery_style', '__return_false' );
  103. }
  104. endif; // twentyfourteen_setup
  105. add_action( 'after_setup_theme', 'twentyfourteen_setup' );
  106. /**
  107. * Adjust content_width value for image attachment template.
  108. *
  109. * @since Twenty Fourteen 1.0
  110. */
  111. function twentyfourteen_content_width() {
  112. if ( is_attachment() && wp_attachment_is_image() ) {
  113. $GLOBALS['content_width'] = 810;
  114. }
  115. }
  116. add_action( 'template_redirect', 'twentyfourteen_content_width' );
  117. /**
  118. * Getter function for Featured Content Plugin.
  119. *
  120. * @since Twenty Fourteen 1.0
  121. *
  122. * @return array An array of WP_Post objects.
  123. */
  124. function twentyfourteen_get_featured_posts() {
  125. /**
  126. * Filter the featured posts to return in Twenty Fourteen.
  127. *
  128. * @since Twenty Fourteen 1.0
  129. *
  130. * @param array|bool $posts Array of featured posts, otherwise false.
  131. */
  132. return apply_filters( 'twentyfourteen_get_featured_posts', array() );
  133. }
  134. /**
  135. * A helper conditional function that returns a boolean value.
  136. *
  137. * @since Twenty Fourteen 1.0
  138. *
  139. * @return bool Whether there are featured posts.
  140. */
  141. function twentyfourteen_has_featured_posts() {
  142. return ! is_paged() && (bool) twentyfourteen_get_featured_posts();
  143. }
  144. /**
  145. * Register three Twenty Fourteen widget areas.
  146. *
  147. * @since Twenty Fourteen 1.0
  148. */
  149. function twentyfourteen_widgets_init() {
  150. require get_template_directory() . '/inc/widgets.php';
  151. register_widget( 'Twenty_Fourteen_Ephemera_Widget' );
  152. register_sidebar( array(
  153. 'name' => __( 'Primary Sidebar', 'twentyfourteen' ),
  154. 'id' => 'sidebar-1',
  155. 'description' => __( 'Main sidebar that appears on the left.', 'twentyfourteen' ),
  156. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  157. 'after_widget' => '</aside>',
  158. 'before_title' => '<h1 class="widget-title">',
  159. 'after_title' => '</h1>',
  160. ) );
  161. register_sidebar( array(
  162. 'name' => __( 'Content Sidebar', 'twentyfourteen' ),
  163. 'id' => 'sidebar-2',
  164. 'description' => __( 'Additional sidebar that appears on the right.', 'twentyfourteen' ),
  165. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  166. 'after_widget' => '</aside>',
  167. 'before_title' => '<h1 class="widget-title">',
  168. 'after_title' => '</h1>',
  169. ) );
  170. register_sidebar( array(
  171. 'name' => __( 'Footer Widget Area', 'twentyfourteen' ),
  172. 'id' => 'sidebar-3',
  173. 'description' => __( 'Appears in the footer section of the site.', 'twentyfourteen' ),
  174. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  175. 'after_widget' => '</aside>',
  176. 'before_title' => '<h1 class="widget-title">',
  177. 'after_title' => '</h1>',
  178. ) );
  179. }
  180. add_action( 'widgets_init', 'twentyfourteen_widgets_init' );
  181. /**
  182. * Register Lato Google font for Twenty Fourteen.
  183. *
  184. * @since Twenty Fourteen 1.0
  185. *
  186. * @return string
  187. */
  188. function twentyfourteen_font_url() {
  189. $font_url = '';
  190. /*
  191. * Translators: If there are characters in your language that are not supported
  192. * by Lato, translate this to 'off'. Do not translate into your own language.
  193. */
  194. if ( 'off' !== _x( 'on', 'Lato font: on or off', 'twentyfourteen' ) ) {
  195. $query_args = array(
  196. 'family' => urlencode( 'Lato:300,400,700,900,300italic,400italic,700italic' ),
  197. 'subset' => urlencode( 'latin,latin-ext' ),
  198. );
  199. $font_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  200. }
  201. return $font_url;
  202. }
  203. /**
  204. * Enqueue scripts and styles for the front end.
  205. *
  206. * @since Twenty Fourteen 1.0
  207. */
  208. function twentyfourteen_scripts() {
  209. // Add Lato font, used in the main stylesheet.
  210. wp_enqueue_style( 'twentyfourteen-lato', twentyfourteen_font_url(), array(), null );
  211. // Add Genericons font, used in the main stylesheet.
  212. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.0.3' );
  213. // Load our main stylesheet.
  214. wp_enqueue_style( 'twentyfourteen-style', get_stylesheet_uri() );
  215. // Load the Internet Explorer specific stylesheet.
  216. wp_enqueue_style( 'twentyfourteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfourteen-style' ), '20131205' );
  217. wp_style_add_data( 'twentyfourteen-ie', 'conditional', 'lt IE 9' );
  218. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  219. wp_enqueue_script( 'comment-reply' );
  220. }
  221. if ( is_singular() && wp_attachment_is_image() ) {
  222. wp_enqueue_script( 'twentyfourteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20130402' );
  223. }
  224. if ( is_active_sidebar( 'sidebar-3' ) ) {
  225. wp_enqueue_script( 'jquery-masonry' );
  226. }
  227. if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) {
  228. wp_enqueue_script( 'twentyfourteen-slider', get_template_directory_uri() . '/js/slider.js', array( 'jquery' ), '20131205', true );
  229. wp_localize_script( 'twentyfourteen-slider', 'featuredSliderDefaults', array(
  230. 'prevText' => __( 'Previous', 'twentyfourteen' ),
  231. 'nextText' => __( 'Next', 'twentyfourteen' )
  232. ) );
  233. }
  234. wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150315', true );
  235. }
  236. add_action( 'wp_enqueue_scripts', 'twentyfourteen_scripts' );
  237. /**
  238. * Enqueue Google fonts style to admin screen for custom header display.
  239. *
  240. * @since Twenty Fourteen 1.0
  241. */
  242. function twentyfourteen_admin_fonts() {
  243. wp_enqueue_style( 'twentyfourteen-lato', twentyfourteen_font_url(), array(), null );
  244. }
  245. add_action( 'admin_print_scripts-appearance_page_custom-header', 'twentyfourteen_admin_fonts' );
  246. if ( ! function_exists( 'twentyfourteen_the_attached_image' ) ) :
  247. /**
  248. * Print the attached image with a link to the next attached image.
  249. *
  250. * @since Twenty Fourteen 1.0
  251. */
  252. function twentyfourteen_the_attached_image() {
  253. $post = get_post();
  254. /**
  255. * Filter the default Twenty Fourteen attachment size.
  256. *
  257. * @since Twenty Fourteen 1.0
  258. *
  259. * @param array $dimensions {
  260. * An array of height and width dimensions.
  261. *
  262. * @type int $height Height of the image in pixels. Default 810.
  263. * @type int $width Width of the image in pixels. Default 810.
  264. * }
  265. */
  266. $attachment_size = apply_filters( 'twentyfourteen_attachment_size', array( 810, 810 ) );
  267. $next_attachment_url = wp_get_attachment_url();
  268. /*
  269. * Grab the IDs of all the image attachments in a gallery so we can get the URL
  270. * of the next adjacent image in a gallery, or the first image (if we're
  271. * looking at the last image in a gallery), or, in a gallery of one, just the
  272. * link to that image file.
  273. */
  274. $attachment_ids = get_posts( array(
  275. 'post_parent' => $post->post_parent,
  276. 'fields' => 'ids',
  277. 'numberposts' => -1,
  278. 'post_status' => 'inherit',
  279. 'post_type' => 'attachment',
  280. 'post_mime_type' => 'image',
  281. 'order' => 'ASC',
  282. 'orderby' => 'menu_order ID',
  283. ) );
  284. // If there is more than 1 attachment in a gallery...
  285. if ( count( $attachment_ids ) > 1 ) {
  286. foreach ( $attachment_ids as $attachment_id ) {
  287. if ( $attachment_id == $post->ID ) {
  288. $next_id = current( $attachment_ids );
  289. break;
  290. }
  291. }
  292. // get the URL of the next image attachment...
  293. if ( $next_id ) {
  294. $next_attachment_url = get_attachment_link( $next_id );
  295. }
  296. // or get the URL of the first image attachment.
  297. else {
  298. $next_attachment_url = get_attachment_link( reset( $attachment_ids ) );
  299. }
  300. }
  301. printf( '<a href="%1$s" rel="attachment">%2$s</a>',
  302. esc_url( $next_attachment_url ),
  303. wp_get_attachment_image( $post->ID, $attachment_size )
  304. );
  305. }
  306. endif;
  307. if ( ! function_exists( 'twentyfourteen_list_authors' ) ) :
  308. /**
  309. * Print a list of all site contributors who published at least one post.
  310. *
  311. * @since Twenty Fourteen 1.0
  312. */
  313. function twentyfourteen_list_authors() {
  314. $contributor_ids = get_users( array(
  315. 'fields' => 'ID',
  316. 'orderby' => 'post_count',
  317. 'order' => 'DESC',
  318. 'who' => 'authors',
  319. ) );
  320. foreach ( $contributor_ids as $contributor_id ) :
  321. $post_count = count_user_posts( $contributor_id );
  322. // Move on if user has not published a post (yet).
  323. if ( ! $post_count ) {
  324. continue;
  325. }
  326. ?>
  327. <div class="contributor">
  328. <div class="contributor-info">
  329. <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
  330. <div class="contributor-summary">
  331. <h2 class="contributor-name"><?php echo get_the_author_meta( 'display_name', $contributor_id ); ?></h2>
  332. <p class="contributor-bio">
  333. <?php echo get_the_author_meta( 'description', $contributor_id ); ?>
  334. </p>
  335. <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
  336. <?php printf( _n( '%d Article', '%d Articles', $post_count, 'twentyfourteen' ), $post_count ); ?>
  337. </a>
  338. </div><!-- .contributor-summary -->
  339. </div><!-- .contributor-info -->
  340. </div><!-- .contributor -->
  341. <?php
  342. endforeach;
  343. }
  344. endif;
  345. /**
  346. * Extend the default WordPress body classes.
  347. *
  348. * Adds body classes to denote:
  349. * 1. Single or multiple authors.
  350. * 2. Presence of header image except in Multisite signup and activate pages.
  351. * 3. Index views.
  352. * 4. Full-width content layout.
  353. * 5. Presence of footer widgets.
  354. * 6. Single views.
  355. * 7. Featured content layout.
  356. *
  357. * @since Twenty Fourteen 1.0
  358. *
  359. * @param array $classes A list of existing body class values.
  360. * @return array The filtered body class list.
  361. */
  362. function twentyfourteen_body_classes( $classes ) {
  363. if ( is_multi_author() ) {
  364. $classes[] = 'group-blog';
  365. }
  366. if ( get_header_image() ) {
  367. $classes[] = 'header-image';
  368. } elseif ( ! in_array( $GLOBALS['pagenow'], array( 'wp-activate.php', 'wp-signup.php' ) ) ) {
  369. $classes[] = 'masthead-fixed';
  370. }
  371. if ( is_archive() || is_search() || is_home() ) {
  372. $classes[] = 'list-view';
  373. }
  374. if ( ( ! is_active_sidebar( 'sidebar-2' ) )
  375. || is_page_template( 'page-templates/full-width.php' )
  376. || is_page_template( 'page-templates/contributors.php' )
  377. || is_attachment() ) {
  378. $classes[] = 'full-width';
  379. }
  380. if ( is_active_sidebar( 'sidebar-3' ) ) {
  381. $classes[] = 'footer-widgets';
  382. }
  383. if ( is_singular() && ! is_front_page() ) {
  384. $classes[] = 'singular';
  385. }
  386. if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) {
  387. $classes[] = 'slider';
  388. } elseif ( is_front_page() ) {
  389. $classes[] = 'grid';
  390. }
  391. return $classes;
  392. }
  393. add_filter( 'body_class', 'twentyfourteen_body_classes' );
  394. /**
  395. * Extend the default WordPress post classes.
  396. *
  397. * Adds a post class to denote:
  398. * Non-password protected page with a post thumbnail.
  399. *
  400. * @since Twenty Fourteen 1.0
  401. *
  402. * @param array $classes A list of existing post class values.
  403. * @return array The filtered post class list.
  404. */
  405. function twentyfourteen_post_classes( $classes ) {
  406. if ( ! post_password_required() && ! is_attachment() && has_post_thumbnail() ) {
  407. $classes[] = 'has-post-thumbnail';
  408. }
  409. return $classes;
  410. }
  411. add_filter( 'post_class', 'twentyfourteen_post_classes' );
  412. /**
  413. * Create a nicely formatted and more specific title element text for output
  414. * in head of document, based on current view.
  415. *
  416. * @since Twenty Fourteen 1.0
  417. *
  418. * @global int $paged WordPress archive pagination page count.
  419. * @global int $page WordPress paginated post page count.
  420. *
  421. * @param string $title Default title text for current view.
  422. * @param string $sep Optional separator.
  423. * @return string The filtered title.
  424. */
  425. function twentyfourteen_wp_title( $title, $sep ) {
  426. global $paged, $page;
  427. if ( is_feed() ) {
  428. return $title;
  429. }
  430. // Add the site name.
  431. $title .= get_bloginfo( 'name', 'display' );
  432. // Add the site description for the home/front page.
  433. $site_description = get_bloginfo( 'description', 'display' );
  434. if ( $site_description && ( is_home() || is_front_page() ) ) {
  435. $title = "$title $sep $site_description";
  436. }
  437. // Add a page number if necessary.
  438. if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
  439. $title = "$title $sep " . sprintf( __( 'Page %s', 'twentyfourteen' ), max( $paged, $page ) );
  440. }
  441. return $title;
  442. }
  443. add_filter( 'wp_title', 'twentyfourteen_wp_title', 10, 2 );
  444. // Implement Custom Header features.
  445. require get_template_directory() . '/inc/custom-header.php';
  446. // Custom template tags for this theme.
  447. require get_template_directory() . '/inc/template-tags.php';
  448. // Add Customizer functionality.
  449. require get_template_directory() . '/inc/customizer.php';
  450. /*
  451. * Add Featured Content functionality.
  452. *
  453. * To overwrite in a plugin, define your own Featured_Content class on or
  454. * before the 'setup_theme' hook.
  455. */
  456. if ( ! class_exists( 'Featured_Content' ) && 'plugins.php' !== $GLOBALS['pagenow'] ) {
  457. require get_template_directory() . '/inc/featured-content.php';
  458. }