functions.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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() ) );
  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',
  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. * @return void
  112. */
  113. function twentyfourteen_content_width() {
  114. if ( is_attachment() && wp_attachment_is_image() ) {
  115. $GLOBALS['content_width'] = 810;
  116. }
  117. }
  118. add_action( 'template_redirect', 'twentyfourteen_content_width' );
  119. /**
  120. * Getter function for Featured Content Plugin.
  121. *
  122. * @since Twenty Fourteen 1.0
  123. *
  124. * @return array An array of WP_Post objects.
  125. */
  126. function twentyfourteen_get_featured_posts() {
  127. /**
  128. * Filter the featured posts to return in Twenty Fourteen.
  129. *
  130. * @since Twenty Fourteen 1.0
  131. *
  132. * @param array|bool $posts Array of featured posts, otherwise false.
  133. */
  134. return apply_filters( 'twentyfourteen_get_featured_posts', array() );
  135. }
  136. /**
  137. * A helper conditional function that returns a boolean value.
  138. *
  139. * @since Twenty Fourteen 1.0
  140. *
  141. * @return bool Whether there are featured posts.
  142. */
  143. function twentyfourteen_has_featured_posts() {
  144. return ! is_paged() && (bool) twentyfourteen_get_featured_posts();
  145. }
  146. /**
  147. * Register three Twenty Fourteen widget areas.
  148. *
  149. * @since Twenty Fourteen 1.0
  150. *
  151. * @return void
  152. */
  153. function twentyfourteen_widgets_init() {
  154. require get_template_directory() . '/inc/widgets.php';
  155. register_widget( 'Twenty_Fourteen_Ephemera_Widget' );
  156. register_sidebar( array(
  157. 'name' => __( 'Primary Sidebar', 'twentyfourteen' ),
  158. 'id' => 'sidebar-1',
  159. 'description' => __( 'Main sidebar that appears on the left.', 'twentyfourteen' ),
  160. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  161. 'after_widget' => '</aside>',
  162. 'before_title' => '<h1 class="widget-title">',
  163. 'after_title' => '</h1>',
  164. ) );
  165. register_sidebar( array(
  166. 'name' => __( 'Content Sidebar', 'twentyfourteen' ),
  167. 'id' => 'sidebar-2',
  168. 'description' => __( 'Additional sidebar that appears on the right.', 'twentyfourteen' ),
  169. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  170. 'after_widget' => '</aside>',
  171. 'before_title' => '<h1 class="widget-title">',
  172. 'after_title' => '</h1>',
  173. ) );
  174. register_sidebar( array(
  175. 'name' => __( 'Footer Widget Area', 'twentyfourteen' ),
  176. 'id' => 'sidebar-3',
  177. 'description' => __( 'Appears in the footer section of the site.', 'twentyfourteen' ),
  178. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  179. 'after_widget' => '</aside>',
  180. 'before_title' => '<h1 class="widget-title">',
  181. 'after_title' => '</h1>',
  182. ) );
  183. }
  184. add_action( 'widgets_init', 'twentyfourteen_widgets_init' );
  185. /**
  186. * Register Lato Google font for Twenty Fourteen.
  187. *
  188. * @since Twenty Fourteen 1.0
  189. *
  190. * @return string
  191. */
  192. function twentyfourteen_font_url() {
  193. $font_url = '';
  194. /*
  195. * Translators: If there are characters in your language that are not supported
  196. * by Lato, translate this to 'off'. Do not translate into your own language.
  197. */
  198. if ( 'off' !== _x( 'on', 'Lato font: on or off', 'twentyfourteen' ) ) {
  199. $font_url = add_query_arg( 'family', urlencode( 'Lato:300,400,700,900,300italic,400italic,700italic' ), "//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. * @return void
  209. */
  210. function twentyfourteen_scripts() {
  211. // Add Lato font, used in the main stylesheet.
  212. wp_enqueue_style( 'twentyfourteen-lato', twentyfourteen_font_url(), array(), null );
  213. // Add Genericons font, used in the main stylesheet.
  214. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.0.2' );
  215. // Load our main stylesheet.
  216. wp_enqueue_style( 'twentyfourteen-style', get_stylesheet_uri(), array( 'genericons' ) );
  217. // Load the Internet Explorer specific stylesheet.
  218. wp_enqueue_style( 'twentyfourteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfourteen-style', 'genericons' ), '20131205' );
  219. wp_style_add_data( 'twentyfourteen-ie', 'conditional', 'lt IE 9' );
  220. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  221. wp_enqueue_script( 'comment-reply' );
  222. }
  223. if ( is_singular() && wp_attachment_is_image() ) {
  224. wp_enqueue_script( 'twentyfourteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20130402' );
  225. }
  226. if ( is_active_sidebar( 'sidebar-3' ) ) {
  227. wp_enqueue_script( 'jquery-masonry' );
  228. }
  229. if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) {
  230. wp_enqueue_script( 'twentyfourteen-slider', get_template_directory_uri() . '/js/slider.js', array( 'jquery' ), '20131205', true );
  231. wp_localize_script( 'twentyfourteen-slider', 'featuredSliderDefaults', array(
  232. 'prevText' => __( 'Previous', 'twentyfourteen' ),
  233. 'nextText' => __( 'Next', 'twentyfourteen' )
  234. ) );
  235. }
  236. wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20131209', true );
  237. }
  238. add_action( 'wp_enqueue_scripts', 'twentyfourteen_scripts' );
  239. /**
  240. * Enqueue Google fonts style to admin screen for custom header display.
  241. *
  242. * @since Twenty Fourteen 1.0
  243. *
  244. * @return void
  245. */
  246. function twentyfourteen_admin_fonts() {
  247. wp_enqueue_style( 'twentyfourteen-lato', twentyfourteen_font_url(), array(), null );
  248. }
  249. add_action( 'admin_print_scripts-appearance_page_custom-header', 'twentyfourteen_admin_fonts' );
  250. if ( ! function_exists( 'twentyfourteen_the_attached_image' ) ) :
  251. /**
  252. * Print the attached image with a link to the next attached image.
  253. *
  254. * @since Twenty Fourteen 1.0
  255. *
  256. * @return void
  257. */
  258. function twentyfourteen_the_attached_image() {
  259. $post = get_post();
  260. /**
  261. * Filter the default Twenty Fourteen attachment size.
  262. *
  263. * @since Twenty Fourteen 1.0
  264. *
  265. * @param array $dimensions {
  266. * An array of height and width dimensions.
  267. *
  268. * @type int $height Height of the image in pixels. Default 810.
  269. * @type int $width Width of the image in pixels. Default 810.
  270. * }
  271. */
  272. $attachment_size = apply_filters( 'twentyfourteen_attachment_size', array( 810, 810 ) );
  273. $next_attachment_url = wp_get_attachment_url();
  274. /*
  275. * Grab the IDs of all the image attachments in a gallery so we can get the URL
  276. * of the next adjacent image in a gallery, or the first image (if we're
  277. * looking at the last image in a gallery), or, in a gallery of one, just the
  278. * link to that image file.
  279. */
  280. $attachment_ids = get_posts( array(
  281. 'post_parent' => $post->post_parent,
  282. 'fields' => 'ids',
  283. 'numberposts' => -1,
  284. 'post_status' => 'inherit',
  285. 'post_type' => 'attachment',
  286. 'post_mime_type' => 'image',
  287. 'order' => 'ASC',
  288. 'orderby' => 'menu_order ID',
  289. ) );
  290. // If there is more than 1 attachment in a gallery...
  291. if ( count( $attachment_ids ) > 1 ) {
  292. foreach ( $attachment_ids as $attachment_id ) {
  293. if ( $attachment_id == $post->ID ) {
  294. $next_id = current( $attachment_ids );
  295. break;
  296. }
  297. }
  298. // get the URL of the next image attachment...
  299. if ( $next_id ) {
  300. $next_attachment_url = get_attachment_link( $next_id );
  301. }
  302. // or get the URL of the first image attachment.
  303. else {
  304. $next_attachment_url = get_attachment_link( array_shift( $attachment_ids ) );
  305. }
  306. }
  307. printf( '<a href="%1$s" rel="attachment">%2$s</a>',
  308. esc_url( $next_attachment_url ),
  309. wp_get_attachment_image( $post->ID, $attachment_size )
  310. );
  311. }
  312. endif;
  313. if ( ! function_exists( 'twentyfourteen_list_authors' ) ) :
  314. /**
  315. * Print a list of all site contributors who published at least one post.
  316. *
  317. * @since Twenty Fourteen 1.0
  318. *
  319. * @return void
  320. */
  321. function twentyfourteen_list_authors() {
  322. $contributor_ids = get_users( array(
  323. 'fields' => 'ID',
  324. 'orderby' => 'post_count',
  325. 'order' => 'DESC',
  326. 'who' => 'authors',
  327. ) );
  328. foreach ( $contributor_ids as $contributor_id ) :
  329. $post_count = count_user_posts( $contributor_id );
  330. // Move on if user has not published a post (yet).
  331. if ( ! $post_count ) {
  332. continue;
  333. }
  334. ?>
  335. <div class="contributor">
  336. <div class="contributor-info">
  337. <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
  338. <div class="contributor-summary">
  339. <h2 class="contributor-name"><?php echo get_the_author_meta( 'display_name', $contributor_id ); ?></h2>
  340. <p class="contributor-bio">
  341. <?php echo get_the_author_meta( 'description', $contributor_id ); ?>
  342. </p>
  343. <a class="contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
  344. <?php printf( _n( '%d Article', '%d Articles', $post_count, 'twentyfourteen' ), $post_count ); ?>
  345. </a>
  346. </div><!-- .contributor-summary -->
  347. </div><!-- .contributor-info -->
  348. </div><!-- .contributor -->
  349. <?php
  350. endforeach;
  351. }
  352. endif;
  353. /**
  354. * Extend the default WordPress body classes.
  355. *
  356. * Adds body classes to denote:
  357. * 1. Single or multiple authors.
  358. * 2. Presence of header image.
  359. * 3. Index views.
  360. * 4. Full-width content layout.
  361. * 5. Presence of footer widgets.
  362. * 6. Single views.
  363. * 7. Featured content layout.
  364. *
  365. * @since Twenty Fourteen 1.0
  366. *
  367. * @param array $classes A list of existing body class values.
  368. * @return array The filtered body class list.
  369. */
  370. function twentyfourteen_body_classes( $classes ) {
  371. if ( is_multi_author() ) {
  372. $classes[] = 'group-blog';
  373. }
  374. if ( get_header_image() ) {
  375. $classes[] = 'header-image';
  376. } else {
  377. $classes[] = 'masthead-fixed';
  378. }
  379. if ( is_archive() || is_search() || is_home() ) {
  380. $classes[] = 'list-view';
  381. }
  382. if ( ( ! is_active_sidebar( 'sidebar-2' ) )
  383. || is_page_template( 'page-templates/full-width.php' )
  384. || is_page_template( 'page-templates/contributors.php' )
  385. || is_attachment() ) {
  386. $classes[] = 'full-width';
  387. }
  388. if ( is_active_sidebar( 'sidebar-3' ) ) {
  389. $classes[] = 'footer-widgets';
  390. }
  391. if ( is_singular() && ! is_front_page() ) {
  392. $classes[] = 'singular';
  393. }
  394. if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) {
  395. $classes[] = 'slider';
  396. } elseif ( is_front_page() ) {
  397. $classes[] = 'grid';
  398. }
  399. return $classes;
  400. }
  401. add_filter( 'body_class', 'twentyfourteen_body_classes' );
  402. /**
  403. * Extend the default WordPress post classes.
  404. *
  405. * Adds a post class to denote:
  406. * Non-password protected page with a post thumbnail.
  407. *
  408. * @since Twenty Fourteen 1.0
  409. *
  410. * @param array $classes A list of existing post class values.
  411. * @return array The filtered post class list.
  412. */
  413. function twentyfourteen_post_classes( $classes ) {
  414. if ( ! post_password_required() && has_post_thumbnail() ) {
  415. $classes[] = 'has-post-thumbnail';
  416. }
  417. return $classes;
  418. }
  419. add_filter( 'post_class', 'twentyfourteen_post_classes' );
  420. /**
  421. * Create a nicely formatted and more specific title element text for output
  422. * in head of document, based on current view.
  423. *
  424. * @since Twenty Fourteen 1.0
  425. *
  426. * @param string $title Default title text for current view.
  427. * @param string $sep Optional separator.
  428. * @return string The filtered title.
  429. */
  430. function twentyfourteen_wp_title( $title, $sep ) {
  431. global $paged, $page;
  432. if ( is_feed() ) {
  433. return $title;
  434. }
  435. // Add the site name.
  436. $title .= get_bloginfo( 'name' );
  437. // Add the site description for the home/front page.
  438. $site_description = get_bloginfo( 'description', 'display' );
  439. if ( $site_description && ( is_home() || is_front_page() ) ) {
  440. $title = "$title $sep $site_description";
  441. }
  442. // Add a page number if necessary.
  443. if ( $paged >= 2 || $page >= 2 ) {
  444. $title = "$title $sep " . sprintf( __( 'Page %s', 'twentyfourteen' ), max( $paged, $page ) );
  445. }
  446. return $title;
  447. }
  448. add_filter( 'wp_title', 'twentyfourteen_wp_title', 10, 2 );
  449. // Implement Custom Header features.
  450. require get_template_directory() . '/inc/custom-header.php';
  451. // Custom template tags for this theme.
  452. require get_template_directory() . '/inc/template-tags.php';
  453. // Add Theme Customizer functionality.
  454. require get_template_directory() . '/inc/customizer.php';
  455. /*
  456. * Add Featured Content functionality.
  457. *
  458. * To overwrite in a plugin, define your own Featured_Content class on or
  459. * before the 'setup_theme' hook.
  460. */
  461. if ( ! class_exists( 'Featured_Content' ) && 'plugins.php' !== $GLOBALS['pagenow'] ) {
  462. require get_template_directory() . '/inc/featured-content.php';
  463. }