~canonical-sysadmins/wordpress/4.7.4

« back to all changes in this revision

Viewing changes to wp-content/themes/twentyfourteen/functions.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

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