3 * Twenty Fourteen functions and definitions
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.
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.
14 * @link https://codex.wordpress.org/Theme_Development
15 * @link https://codex.wordpress.org/Child_Themes
17 * Functions that are not pluggable (not wrapped in function_exists()) are
18 * instead attached to a filter or action hook.
20 * For more information on hooks, actions, and filters,
21 * @link https://codex.wordpress.org/Plugin_API
24 * @subpackage Twenty_Fourteen
25 * @since Twenty Fourteen 1.0
29 * Set up the content width value based on the theme's design.
31 * @see twentyfourteen_content_width()
33 * @since Twenty Fourteen 1.0
35 if ( ! isset( $content_width ) ) {
40 * Twenty Fourteen only works in WordPress 3.6 or later.
42 if ( version_compare( $GLOBALS['wp_version'], '3.6', '<' ) ) {
43 require get_template_directory() . '/inc/back-compat.php';
46 if ( ! function_exists( 'twentyfourteen_setup' ) ) :
48 * Twenty Fourteen setup.
50 * Set up theme defaults and registers support for various WordPress features.
52 * Note that this function is hooked into the after_setup_theme hook, which
53 * runs before the init hook. The init hook is too late for some features, such
54 * as indicating support post thumbnails.
56 * @since Twenty Fourteen 1.0
58 function twentyfourteen_setup() {
61 * Make Twenty Fourteen available for translation.
63 * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/twentyfourteen
64 * If you're building a theme based on Twenty Fourteen, use a find and
65 * replace to change 'twentyfourteen' to the name of your theme in all
68 load_theme_textdomain( 'twentyfourteen' );
70 // This theme styles the visual editor to resemble the theme style.
71 add_editor_style( array( 'css/editor-style.css', twentyfourteen_font_url(), 'genericons/genericons.css' ) );
73 // Add RSS feed links to <head> for posts and comments.
74 add_theme_support( 'automatic-feed-links' );
76 // Enable support for Post Thumbnails, and declare two sizes.
77 add_theme_support( 'post-thumbnails' );
78 set_post_thumbnail_size( 672, 372, true );
79 add_image_size( 'twentyfourteen-full-width', 1038, 576, true );
81 // This theme uses wp_nav_menu() in two locations.
82 register_nav_menus( array(
83 'primary' => __( 'Top primary menu', 'twentyfourteen' ),
84 'secondary' => __( 'Secondary menu in left sidebar', 'twentyfourteen' ),
88 * Switch default core markup for search form, comment form, and comments
89 * to output valid HTML5.
91 add_theme_support( 'html5', array(
92 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
96 * Enable support for Post Formats.
97 * See https://codex.wordpress.org/Post_Formats
99 add_theme_support( 'post-formats', array(
100 'aside', 'image', 'video', 'audio', 'quote', 'link', 'gallery',
103 // This theme allows users to set a custom background.
104 add_theme_support( 'custom-background', apply_filters( 'twentyfourteen_custom_background_args', array(
105 'default-color' => 'f5f5f5',
108 // Add support for featured content.
109 add_theme_support( 'featured-content', array(
110 'featured_content_filter' => 'twentyfourteen_get_featured_posts',
114 // This theme uses its own gallery styles.
115 add_filter( 'use_default_gallery_style', '__return_false' );
117 // Indicate widget sidebars can use selective refresh in the Customizer.
118 add_theme_support( 'customize-selective-refresh-widgets' );
120 endif; // twentyfourteen_setup
121 add_action( 'after_setup_theme', 'twentyfourteen_setup' );
124 * Adjust content_width value for image attachment template.
126 * @since Twenty Fourteen 1.0
128 function twentyfourteen_content_width() {
129 if ( is_attachment() && wp_attachment_is_image() ) {
130 $GLOBALS['content_width'] = 810;
133 add_action( 'template_redirect', 'twentyfourteen_content_width' );
136 * Getter function for Featured Content Plugin.
138 * @since Twenty Fourteen 1.0
140 * @return array An array of WP_Post objects.
142 function twentyfourteen_get_featured_posts() {
144 * Filter the featured posts to return in Twenty Fourteen.
146 * @since Twenty Fourteen 1.0
148 * @param array|bool $posts Array of featured posts, otherwise false.
150 return apply_filters( 'twentyfourteen_get_featured_posts', array() );
154 * A helper conditional function that returns a boolean value.
156 * @since Twenty Fourteen 1.0
158 * @return bool Whether there are featured posts.
160 function twentyfourteen_has_featured_posts() {
161 return ! is_paged() && (bool) twentyfourteen_get_featured_posts();
165 * Register three Twenty Fourteen widget areas.
167 * @since Twenty Fourteen 1.0
169 function twentyfourteen_widgets_init() {
170 require get_template_directory() . '/inc/widgets.php';
171 register_widget( 'Twenty_Fourteen_Ephemera_Widget' );
173 register_sidebar( array(
174 'name' => __( 'Primary Sidebar', 'twentyfourteen' ),
176 'description' => __( 'Main sidebar that appears on the left.', 'twentyfourteen' ),
177 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
178 'after_widget' => '</aside>',
179 'before_title' => '<h1 class="widget-title">',
180 'after_title' => '</h1>',
182 register_sidebar( array(
183 'name' => __( 'Content Sidebar', 'twentyfourteen' ),
185 'description' => __( 'Additional sidebar that appears on the right.', 'twentyfourteen' ),
186 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
187 'after_widget' => '</aside>',
188 'before_title' => '<h1 class="widget-title">',
189 'after_title' => '</h1>',
191 register_sidebar( array(
192 'name' => __( 'Footer Widget Area', 'twentyfourteen' ),
194 'description' => __( 'Appears in the footer section of the site.', 'twentyfourteen' ),
195 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
196 'after_widget' => '</aside>',
197 'before_title' => '<h1 class="widget-title">',
198 'after_title' => '</h1>',
201 add_action( 'widgets_init', 'twentyfourteen_widgets_init' );
204 * Register Lato Google font for Twenty Fourteen.
206 * @since Twenty Fourteen 1.0
210 function twentyfourteen_font_url() {
213 * Translators: If there are characters in your language that are not supported
214 * by Lato, translate this to 'off'. Do not translate into your own language.
216 if ( 'off' !== _x( 'on', 'Lato font: on or off', 'twentyfourteen' ) ) {
218 'family' => urlencode( 'Lato:300,400,700,900,300italic,400italic,700italic' ),
219 'subset' => urlencode( 'latin,latin-ext' ),
221 $font_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
228 * Enqueue scripts and styles for the front end.
230 * @since Twenty Fourteen 1.0
232 function twentyfourteen_scripts() {
233 // Add Lato font, used in the main stylesheet.
234 wp_enqueue_style( 'twentyfourteen-lato', twentyfourteen_font_url(), array(), null );
236 // Add Genericons font, used in the main stylesheet.
237 wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.0.3' );
239 // Load our main stylesheet.
240 wp_enqueue_style( 'twentyfourteen-style', get_stylesheet_uri() );
242 // Load the Internet Explorer specific stylesheet.
243 wp_enqueue_style( 'twentyfourteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfourteen-style' ), '20131205' );
244 wp_style_add_data( 'twentyfourteen-ie', 'conditional', 'lt IE 9' );
246 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
247 wp_enqueue_script( 'comment-reply' );
250 if ( is_singular() && wp_attachment_is_image() ) {
251 wp_enqueue_script( 'twentyfourteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20130402' );
254 if ( is_active_sidebar( 'sidebar-3' ) ) {
255 wp_enqueue_script( 'jquery-masonry' );
258 if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) {
259 wp_enqueue_script( 'twentyfourteen-slider', get_template_directory_uri() . '/js/slider.js', array( 'jquery' ), '20131205', true );
260 wp_localize_script( 'twentyfourteen-slider', 'featuredSliderDefaults', array(
261 'prevText' => __( 'Previous', 'twentyfourteen' ),
262 'nextText' => __( 'Next', 'twentyfourteen' )
266 wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150315', true );
268 add_action( 'wp_enqueue_scripts', 'twentyfourteen_scripts' );
271 * Enqueue Google fonts style to admin screen for custom header display.
273 * @since Twenty Fourteen 1.0
275 function twentyfourteen_admin_fonts() {
276 wp_enqueue_style( 'twentyfourteen-lato', twentyfourteen_font_url(), array(), null );
278 add_action( 'admin_print_scripts-appearance_page_custom-header', 'twentyfourteen_admin_fonts' );
280 if ( ! function_exists( 'twentyfourteen_the_attached_image' ) ) :
282 * Print the attached image with a link to the next attached image.
284 * @since Twenty Fourteen 1.0
286 function twentyfourteen_the_attached_image() {
289 * Filter the default Twenty Fourteen attachment size.
291 * @since Twenty Fourteen 1.0
293 * @param array $dimensions {
294 * An array of height and width dimensions.
296 * @type int $height Height of the image in pixels. Default 810.
297 * @type int $width Width of the image in pixels. Default 810.
300 $attachment_size = apply_filters( 'twentyfourteen_attachment_size', array( 810, 810 ) );
301 $next_attachment_url = wp_get_attachment_url();
304 * Grab the IDs of all the image attachments in a gallery so we can get the URL
305 * of the next adjacent image in a gallery, or the first image (if we're
306 * looking at the last image in a gallery), or, in a gallery of one, just the
307 * link to that image file.
309 $attachment_ids = get_posts( array(
310 'post_parent' => $post->post_parent,
313 'post_status' => 'inherit',
314 'post_type' => 'attachment',
315 'post_mime_type' => 'image',
317 'orderby' => 'menu_order ID',
320 // If there is more than 1 attachment in a gallery...
321 if ( count( $attachment_ids ) > 1 ) {
322 foreach ( $attachment_ids as $idx => $attachment_id ) {
323 if ( $attachment_id == $post->ID ) {
324 $next_id = $attachment_ids[ ( $idx + 1 ) % count( $attachment_ids ) ];
329 // get the URL of the next image attachment...
331 $next_attachment_url = get_attachment_link( $next_id );
334 // or get the URL of the first image attachment.
336 $next_attachment_url = get_attachment_link( reset( $attachment_ids ) );
340 printf( '<a href="%1$s" rel="attachment">%2$s</a>',
341 esc_url( $next_attachment_url ),
342 wp_get_attachment_image( $post->ID, $attachment_size )
347 if ( ! function_exists( 'twentyfourteen_list_authors' ) ) :
349 * Print a list of all site contributors who published at least one post.
351 * @since Twenty Fourteen 1.0
353 function twentyfourteen_list_authors() {
354 $contributor_ids = get_users( array(
356 'orderby' => 'post_count',
361 foreach ( $contributor_ids as $contributor_id ) :
362 $post_count = count_user_posts( $contributor_id );
364 // Move on if user has not published a post (yet).
365 if ( ! $post_count ) {
370 <div class="contributor">
371 <div class="contributor-info">
372 <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
373 <div class="contributor-summary">
374 <h2 class="contributor-name"><?php echo get_the_author_meta( 'display_name', $contributor_id ); ?></h2>
375 <p class="contributor-bio">
376 <?php echo get_the_author_meta( 'description', $contributor_id ); ?>
378 <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
379 <?php printf( _n( '%d Article', '%d Articles', $post_count, 'twentyfourteen' ), $post_count ); ?>
381 </div><!-- .contributor-summary -->
382 </div><!-- .contributor-info -->
383 </div><!-- .contributor -->
391 * Extend the default WordPress body classes.
393 * Adds body classes to denote:
394 * 1. Single or multiple authors.
395 * 2. Presence of header image except in Multisite signup and activate pages.
397 * 4. Full-width content layout.
398 * 5. Presence of footer widgets.
400 * 7. Featured content layout.
402 * @since Twenty Fourteen 1.0
404 * @param array $classes A list of existing body class values.
405 * @return array The filtered body class list.
407 function twentyfourteen_body_classes( $classes ) {
408 if ( is_multi_author() ) {
409 $classes[] = 'group-blog';
412 if ( get_header_image() ) {
413 $classes[] = 'header-image';
414 } elseif ( ! in_array( $GLOBALS['pagenow'], array( 'wp-activate.php', 'wp-signup.php' ) ) ) {
415 $classes[] = 'masthead-fixed';
418 if ( is_archive() || is_search() || is_home() ) {
419 $classes[] = 'list-view';
422 if ( ( ! is_active_sidebar( 'sidebar-2' ) )
423 || is_page_template( 'page-templates/full-width.php' )
424 || is_page_template( 'page-templates/contributors.php' )
425 || is_attachment() ) {
426 $classes[] = 'full-width';
429 if ( is_active_sidebar( 'sidebar-3' ) ) {
430 $classes[] = 'footer-widgets';
433 if ( is_singular() && ! is_front_page() ) {
434 $classes[] = 'singular';
437 if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) {
438 $classes[] = 'slider';
439 } elseif ( is_front_page() ) {
445 add_filter( 'body_class', 'twentyfourteen_body_classes' );
448 * Extend the default WordPress post classes.
450 * Adds a post class to denote:
451 * Non-password protected page with a post thumbnail.
453 * @since Twenty Fourteen 1.0
455 * @param array $classes A list of existing post class values.
456 * @return array The filtered post class list.
458 function twentyfourteen_post_classes( $classes ) {
459 if ( ! post_password_required() && ! is_attachment() && has_post_thumbnail() ) {
460 $classes[] = 'has-post-thumbnail';
465 add_filter( 'post_class', 'twentyfourteen_post_classes' );
468 * Create a nicely formatted and more specific title element text for output
469 * in head of document, based on current view.
471 * @since Twenty Fourteen 1.0
473 * @global int $paged WordPress archive pagination page count.
474 * @global int $page WordPress paginated post page count.
476 * @param string $title Default title text for current view.
477 * @param string $sep Optional separator.
478 * @return string The filtered title.
480 function twentyfourteen_wp_title( $title, $sep ) {
481 global $paged, $page;
487 // Add the site name.
488 $title .= get_bloginfo( 'name', 'display' );
490 // Add the site description for the home/front page.
491 $site_description = get_bloginfo( 'description', 'display' );
492 if ( $site_description && ( is_home() || is_front_page() ) ) {
493 $title = "$title $sep $site_description";
496 // Add a page number if necessary.
497 if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
498 $title = "$title $sep " . sprintf( __( 'Page %s', 'twentyfourteen' ), max( $paged, $page ) );
503 add_filter( 'wp_title', 'twentyfourteen_wp_title', 10, 2 );
505 // Implement Custom Header features.
506 require get_template_directory() . '/inc/custom-header.php';
508 // Custom template tags for this theme.
509 require get_template_directory() . '/inc/template-tags.php';
511 // Add Customizer functionality.
512 require get_template_directory() . '/inc/customizer.php';
515 * Add Featured Content functionality.
517 * To overwrite in a plugin, define your own Featured_Content class on or
518 * before the 'setup_theme' hook.
520 if ( ! class_exists( 'Featured_Content' ) && 'plugins.php' !== $GLOBALS['pagenow'] ) {
521 require get_template_directory() . '/inc/featured-content.php';