functions.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 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_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 http://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. $font_url = add_query_arg( 'family', urlencode( 'Lato:300,400,700,900,300italic,400italic,700italic' ), "//fonts.googleapis.com/css" );
  196. }
  197. return $font_url;
  198. }
  199. /**
  200. * Enqueue scripts and styles for the front end.
  201. *
  202. * @since Twenty Fourteen 1.0
  203. */
  204. function twentyfourteen_scripts() {
  205. // Add Lato font, used in the main stylesheet.
  206. wp_enqueue_style( 'twentyfourteen-lato', twentyfourteen_font_url(), array(), null );
  207. // Add Genericons font, used in the main stylesheet.
  208. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.0.3' );
  209. // Load our main stylesheet.
  210. wp_enqueue_style( 'twentyfourteen-style', get_stylesheet_uri(), array( 'genericons' ) );
  211. // Load the Internet Explorer specific stylesheet.
  212. wp_enqueue_style( 'twentyfourteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfourteen-style', 'genericons' ), '20131205' );
  213. wp_style_add_data( 'twentyfourteen-ie', 'conditional', 'lt IE 9' );
  214. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  215. wp_enqueue_script( 'comment-reply' );
  216. }
  217. if ( is_singular() && wp_attachment_is_image() ) {
  218. wp_enqueue_script( 'twentyfourteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20130402' );
  219. }
  220. if ( is_active_sidebar( 'sidebar-3' ) ) {
  221. wp_enqueue_script( 'jquery-masonry' );
  222. }
  223. if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) {
  224. wp_enqueue_script( 'twentyfourteen-slider', get_template_directory_uri() . '/js/slider.js', array( 'jquery' ), '20131205', true );
  225. wp_localize_script( 'twentyfourteen-slider', 'featuredSliderDefaults', array(
  226. 'prevText' => __( 'Previous', 'twentyfourteen' ),
  227. 'nextText' => __( 'Next', 'twentyfourteen' )
  228. ) );
  229. }
  230. wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20140616', true );
  231. }
  232. add_action( 'wp_enqueue_scripts', 'twentyfourteen_scripts' );
  233. /**
  234. * Enqueue Google fonts style to admin screen for custom header display.
  235. *
  236. * @since Twenty Fourteen 1.0
  237. */
  238. function twentyfourteen_admin_fonts() {
  239. wp_enqueue_style( 'twentyfourteen-lato', twentyfourteen_font_url(), array(), null );
  240. }
  241. add_action( 'admin_print_scripts-appearance_page_custom-header', 'twentyfourteen_admin_fonts' );
  242. if ( ! function_exists( 'twentyfourteen_the_attached_image' ) ) :
  243. /**
  244. * Print the attached image with a link to the next attached image.
  245. *
  246. * @since Twenty Fourteen 1.0
  247. */
  248. function twentyfourteen_the_attached_image() {
  249. $post = get_post();
  250. /**
  251. * Filter the default Twenty Fourteen attachment size.
  252. *
  253. * @since Twenty Fourteen 1.0
  254. *
  255. * @param array $dimensions {
  256. * An array of height and width dimensions.
  257. *
  258. * @type int $height Height of the image in pixels. Default 810.
  259. * @type int $width Width of the image in pixels. Default 810.
  260. * }
  261. */
  262. $attachment_size = apply_filters( 'twentyfourteen_attachment_size', array( 810, 810 ) );
  263. $next_attachment_url = wp_get_attachment_url();
  264. /*
  265. * Grab the IDs of all the image attachments in a gallery so we can get the URL
  266. * of the next adjacent image in a gallery, or the first image (if we're
  267. * looking at the last image in a gallery), or, in a gallery of one, just the
  268. * link to that image file.
  269. */
  270. $attachment_ids = get_posts( array(
  271. 'post_parent' => $post->post_parent,
  272. 'fields' => 'ids',
  273. 'numberposts' => -1,
  274. 'post_status' => 'inherit',
  275. 'post_type' => 'attachment',
  276. 'post_mime_type' => 'image',
  277. 'order' => 'ASC',
  278. 'orderby' => 'menu_order ID',
  279. ) );
  280. // If there is more than 1 attachment in a gallery...
  281. if ( count( $attachment_ids ) > 1 ) {
  282. foreach ( $attachment_ids as $attachment_id ) {
  283. if ( $attachment_id == $post->ID ) {
  284. $next_id = current( $attachment_ids );
  285. break;
  286. }
  287. }
  288. // get the URL of the next image attachment...
  289. if ( $next_id ) {
  290. $next_attachment_url = get_attachment_link( $next_id );
  291. }
  292. // or get the URL of the first image attachment.
  293. else {
  294. $next_attachment_url = get_attachment_link( array_shift( $attachment_ids ) );
  295. }
  296. }
  297. printf( '<a href="%1$s" rel="attachment">%2$s</a>',
  298. esc_url( $next_attachment_url ),
  299. wp_get_attachment_image( $post->ID, $attachment_size )
  300. );
  301. }
  302. endif;
  303. if ( ! function_exists( 'twentyfourteen_list_authors' ) ) :
  304. /**
  305. * Print a list of all site contributors who published at least one post.
  306. *
  307. * @since Twenty Fourteen 1.0
  308. */
  309. function twentyfourteen_list_authors() {
  310. $contributor_ids = get_users( array(
  311. 'fields' => 'ID',
  312. 'orderby' => 'post_count',
  313. 'order' => 'DESC',
  314. 'who' => 'authors',
  315. ) );
  316. foreach ( $contributor_ids as $contributor_id ) :
  317. $post_count = count_user_posts( $contributor_id );
  318. // Move on if user has not published a post (yet).
  319. if ( ! $post_count ) {
  320. continue;
  321. }
  322. ?>
  323. <div class="contributor">
  324. <div class="contributor-info">
  325. <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
  326. <div class="contributor-summary">
  327. <h2 class="contributor-name"><?php echo get_the_author_meta( 'display_name', $contributor_id ); ?></h2>
  328. <p class="contributor-bio">
  329. <?php echo get_the_author_meta( 'description', $contributor_id ); ?>
  330. </p>
  331. <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
  332. <?php printf( _n( '%d Article', '%d Articles', $post_count, 'twentyfourteen' ), $post_count ); ?>
  333. </a>
  334. </div><!-- .contributor-summary -->
  335. </div><!-- .contributor-info -->
  336. </div><!-- .contributor -->
  337. <?php
  338. endforeach;
  339. }
  340. endif;
  341. /**
  342. * Extend the default WordPress body classes.
  343. *
  344. * Adds body classes to denote:
  345. * 1. Single or multiple authors.
  346. * 2. Presence of header image except in Multisite signup and activate pages.
  347. * 3. Index views.
  348. * 4. Full-width content layout.
  349. * 5. Presence of footer widgets.
  350. * 6. Single views.
  351. * 7. Featured content layout.
  352. *
  353. * @since Twenty Fourteen 1.0
  354. *
  355. * @param array $classes A list of existing body class values.
  356. * @return array The filtered body class list.
  357. */
  358. function twentyfourteen_body_classes( $classes ) {
  359. if ( is_multi_author() ) {
  360. $classes[] = 'group-blog';
  361. }
  362. if ( get_header_image() ) {
  363. $classes[] = 'header-image';
  364. } elseif ( ! in_array( $GLOBALS['pagenow'], array( 'wp-activate.php', 'wp-signup.php' ) ) ) {
  365. $classes[] = 'masthead-fixed';
  366. }
  367. if ( is_archive() || is_search() || is_home() ) {
  368. $classes[] = 'list-view';
  369. }
  370. if ( ( ! is_active_sidebar( 'sidebar-2' ) )
  371. || is_page_template( 'page-templates/full-width.php' )
  372. || is_page_template( 'page-templates/contributors.php' )
  373. || is_attachment() ) {
  374. $classes[] = 'full-width';
  375. }
  376. if ( is_active_sidebar( 'sidebar-3' ) ) {
  377. $classes[] = 'footer-widgets';
  378. }
  379. if ( is_singular() && ! is_front_page() ) {
  380. $classes[] = 'singular';
  381. }
  382. if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) {
  383. $classes[] = 'slider';
  384. } elseif ( is_front_page() ) {
  385. $classes[] = 'grid';
  386. }
  387. return $classes;
  388. }
  389. add_filter( 'body_class', 'twentyfourteen_body_classes' );
  390. /**
  391. * Extend the default WordPress post classes.
  392. *
  393. * Adds a post class to denote:
  394. * Non-password protected page with a post thumbnail.
  395. *
  396. * @since Twenty Fourteen 1.0
  397. *
  398. * @param array $classes A list of existing post class values.
  399. * @return array The filtered post class list.
  400. */
  401. function twentyfourteen_post_classes( $classes ) {
  402. if ( ! post_password_required() && ! is_attachment() && has_post_thumbnail() ) {
  403. $classes[] = 'has-post-thumbnail';
  404. }
  405. return $classes;
  406. }
  407. add_filter( 'post_class', 'twentyfourteen_post_classes' );
  408. /**
  409. * Create a nicely formatted and more specific title element text for output
  410. * in head of document, based on current view.
  411. *
  412. * @since Twenty Fourteen 1.0
  413. *
  414. * @global int $paged WordPress archive pagination page count.
  415. * @global int $page WordPress paginated post page count.
  416. *
  417. * @param string $title Default title text for current view.
  418. * @param string $sep Optional separator.
  419. * @return string The filtered title.
  420. */
  421. function twentyfourteen_wp_title( $title, $sep ) {
  422. global $paged, $page;
  423. if ( is_feed() ) {
  424. return $title;
  425. }
  426. // Add the site name.
  427. $title .= get_bloginfo( 'name', 'display' );
  428. // Add the site description for the home/front page.
  429. $site_description = get_bloginfo( 'description', 'display' );
  430. if ( $site_description && ( is_home() || is_front_page() ) ) {
  431. $title = "$title $sep $site_description";
  432. }
  433. // Add a page number if necessary.
  434. if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
  435. $title = "$title $sep " . sprintf( __( 'Page %s', 'twentyfourteen' ), max( $paged, $page ) );
  436. }
  437. return $title;
  438. }
  439. add_filter( 'wp_title', 'twentyfourteen_wp_title', 10, 2 );
  440. // Implement Custom Header features.
  441. require get_template_directory() . '/inc/custom-header.php';
  442. // Custom template tags for this theme.
  443. require get_template_directory() . '/inc/template-tags.php';
  444. // Add Theme Customizer functionality.
  445. require get_template_directory() . '/inc/customizer.php';
  446. /*
  447. * Add Featured Content functionality.
  448. *
  449. * To overwrite in a plugin, define your own Featured_Content class on or
  450. * before the 'setup_theme' hook.
  451. */
  452. if ( ! class_exists( 'Featured_Content' ) && 'plugins.php' !== $GLOBALS['pagenow'] ) {
  453. require get_template_directory() . '/inc/featured-content.php';
  454. }