]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/themes/twentythirteen/functions.php
Wordpress 3.6
[autoinstalls/wordpress.git] / wp-content / themes / twentythirteen / functions.php
1 <?php
2 /**
3  * Twenty Thirteen functions and definitions.
4  *
5  * Sets 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 (see http://codex.wordpress.org/Theme_Development
10  * and http://codex.wordpress.org/Child_Themes), you can override certain
11  * functions (those wrapped in a function_exists() call) by defining them first
12  * in your child theme's functions.php file. The child theme's functions.php
13  * file is included before the parent theme's file, so the child theme
14  * functions would be used.
15  *
16  * Functions that are not pluggable (not wrapped in function_exists()) are
17  * instead attached to a filter or action hook.
18  *
19  * For more information on hooks, actions, and filters,
20  * see http://codex.wordpress.org/Plugin_API
21  *
22  * @package WordPress
23  * @subpackage Twenty_Thirteen
24  * @since Twenty Thirteen 1.0
25  */
26
27 /**
28  * Sets up the content width value based on the theme's design.
29  * @see twentythirteen_content_width() for template-specific adjustments.
30  */
31 if ( ! isset( $content_width ) )
32         $content_width = 604;
33
34 /**
35  * Adds support for a custom header image.
36  */
37 require get_template_directory() . '/inc/custom-header.php';
38
39 /**
40  * Twenty Thirteen only works in WordPress 3.6 or later.
41  */
42 if ( version_compare( $GLOBALS['wp_version'], '3.6-alpha', '<' ) )
43         require get_template_directory() . '/inc/back-compat.php';
44
45 /**
46  * Sets up theme defaults and registers the various WordPress features that
47  * Twenty Thirteen supports.
48  *
49  * @uses load_theme_textdomain() For translation/localization support.
50  * @uses add_editor_style() To add Visual Editor stylesheets.
51  * @uses add_theme_support() To add support for automatic feed links, post
52  * formats, and post thumbnails.
53  * @uses register_nav_menu() To add support for a navigation menu.
54  * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
55  *
56  * @since Twenty Thirteen 1.0
57  *
58  * @return void
59  */
60 function twentythirteen_setup() {
61         /*
62          * Makes Twenty Thirteen available for translation.
63          *
64          * Translations can be added to the /languages/ directory.
65          * If you're building a theme based on Twenty Thirteen, use a find and
66          * replace to change 'twentythirteen' to the name of your theme in all
67          * template files.
68          */
69         load_theme_textdomain( 'twentythirteen', get_template_directory() . '/languages' );
70
71         /*
72          * This theme styles the visual editor to resemble the theme style,
73          * specifically font, colors, icons, and column width.
74          */
75         add_editor_style( array( 'css/editor-style.css', 'fonts/genericons.css', twentythirteen_fonts_url() ) );
76
77         // Adds RSS feed links to <head> for posts and comments.
78         add_theme_support( 'automatic-feed-links' );
79
80         // Switches default core markup for search form, comment form, and comments
81         // to output valid HTML5.
82         add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list' ) );
83
84         /*
85          * This theme supports all available post formats by default.
86          * See http://codex.wordpress.org/Post_Formats
87          */
88         add_theme_support( 'post-formats', array(
89                 'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video'
90         ) );
91
92         // This theme uses wp_nav_menu() in one location.
93         register_nav_menu( 'primary', __( 'Navigation Menu', 'twentythirteen' ) );
94
95         /*
96          * This theme uses a custom image size for featured images, displayed on
97          * "standard" posts and pages.
98          */
99         add_theme_support( 'post-thumbnails' );
100         set_post_thumbnail_size( 604, 270, true );
101
102         // This theme uses its own gallery styles.
103         add_filter( 'use_default_gallery_style', '__return_false' );
104 }
105 add_action( 'after_setup_theme', 'twentythirteen_setup' );
106
107 /**
108  * Returns the Google font stylesheet URL, if available.
109  *
110  * The use of Source Sans Pro and Bitter by default is localized. For languages
111  * that use characters not supported by the font, the font can be disabled.
112  *
113  * @since Twenty Thirteen 1.0
114  *
115  * @return string Font stylesheet or empty string if disabled.
116  */
117 function twentythirteen_fonts_url() {
118         $fonts_url = '';
119
120         /* Translators: If there are characters in your language that are not
121          * supported by Source Sans Pro, translate this to 'off'. Do not translate
122          * into your own language.
123          */
124         $source_sans_pro = _x( 'on', 'Source Sans Pro font: on or off', 'twentythirteen' );
125
126         /* Translators: If there are characters in your language that are not
127          * supported by Bitter, translate this to 'off'. Do not translate into your
128          * own language.
129          */
130         $bitter = _x( 'on', 'Bitter font: on or off', 'twentythirteen' );
131
132         if ( 'off' !== $source_sans_pro || 'off' !== $bitter ) {
133                 $font_families = array();
134
135                 if ( 'off' !== $source_sans_pro )
136                         $font_families[] = 'Source Sans Pro:300,400,700,300italic,400italic,700italic';
137
138                 if ( 'off' !== $bitter )
139                         $font_families[] = 'Bitter:400,700';
140
141                 $query_args = array(
142                         'family' => urlencode( implode( '|', $font_families ) ),
143                         'subset' => urlencode( 'latin,latin-ext' ),
144                 );
145                 $fonts_url = add_query_arg( $query_args, "//fonts.googleapis.com/css" );
146         }
147
148         return $fonts_url;
149 }
150
151 /**
152  * Enqueues scripts and styles for front end.
153  *
154  * @since Twenty Thirteen 1.0
155  *
156  * @return void
157  */
158 function twentythirteen_scripts_styles() {
159         // Adds JavaScript to pages with the comment form to support sites with
160         // threaded comments (when in use).
161         if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
162                 wp_enqueue_script( 'comment-reply' );
163
164         // Adds Masonry to handle vertical alignment of footer widgets.
165         if ( is_active_sidebar( 'sidebar-1' ) )
166                 wp_enqueue_script( 'jquery-masonry' );
167
168         // Loads JavaScript file with functionality specific to Twenty Thirteen.
169         wp_enqueue_script( 'twentythirteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '2013-07-18', true );
170
171         // Add Open Sans and Bitter fonts, used in the main stylesheet.
172         wp_enqueue_style( 'twentythirteen-fonts', twentythirteen_fonts_url(), array(), null );
173
174         // Add Genericons font, used in the main stylesheet.
175         wp_enqueue_style( 'genericons', get_template_directory_uri() . '/fonts/genericons.css', array(), '2.09' );
176
177         // Loads our main stylesheet.
178         wp_enqueue_style( 'twentythirteen-style', get_stylesheet_uri(), array(), '2013-07-18' );
179
180         // Loads the Internet Explorer specific stylesheet.
181         wp_enqueue_style( 'twentythirteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentythirteen-style' ), '2013-07-18' );
182         wp_style_add_data( 'twentythirteen-ie', 'conditional', 'lt IE 9' );
183 }
184 add_action( 'wp_enqueue_scripts', 'twentythirteen_scripts_styles' );
185
186 /**
187  * Creates a nicely formatted and more specific title element text for output
188  * in head of document, based on current view.
189  *
190  * @since Twenty Thirteen 1.0
191  *
192  * @param string $title Default title text for current view.
193  * @param string $sep Optional separator.
194  * @return string The filtered title.
195  */
196 function twentythirteen_wp_title( $title, $sep ) {
197         global $paged, $page;
198
199         if ( is_feed() )
200                 return $title;
201
202         // Add the site name.
203         $title .= get_bloginfo( 'name' );
204
205         // Add the site description for the home/front page.
206         $site_description = get_bloginfo( 'description', 'display' );
207         if ( $site_description && ( is_home() || is_front_page() ) )
208                 $title = "$title $sep $site_description";
209
210         // Add a page number if necessary.
211         if ( $paged >= 2 || $page >= 2 )
212                 $title = "$title $sep " . sprintf( __( 'Page %s', 'twentythirteen' ), max( $paged, $page ) );
213
214         return $title;
215 }
216 add_filter( 'wp_title', 'twentythirteen_wp_title', 10, 2 );
217
218 /**
219  * Registers two widget areas.
220  *
221  * @since Twenty Thirteen 1.0
222  *
223  * @return void
224  */
225 function twentythirteen_widgets_init() {
226         register_sidebar( array(
227                 'name'          => __( 'Main Widget Area', 'twentythirteen' ),
228                 'id'            => 'sidebar-1',
229                 'description'   => __( 'Appears in the footer section of the site.', 'twentythirteen' ),
230                 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
231                 'after_widget'  => '</aside>',
232                 'before_title'  => '<h3 class="widget-title">',
233                 'after_title'   => '</h3>',
234         ) );
235
236         register_sidebar( array(
237                 'name'          => __( 'Secondary Widget Area', 'twentythirteen' ),
238                 'id'            => 'sidebar-2',
239                 'description'   => __( 'Appears on posts and pages in the sidebar.', 'twentythirteen' ),
240                 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
241                 'after_widget'  => '</aside>',
242                 'before_title'  => '<h3 class="widget-title">',
243                 'after_title'   => '</h3>',
244         ) );
245 }
246 add_action( 'widgets_init', 'twentythirteen_widgets_init' );
247
248 if ( ! function_exists( 'twentythirteen_paging_nav' ) ) :
249 /**
250  * Displays navigation to next/previous set of posts when applicable.
251  *
252  * @since Twenty Thirteen 1.0
253  *
254  * @return void
255  */
256 function twentythirteen_paging_nav() {
257         global $wp_query;
258
259         // Don't print empty markup if there's only one page.
260         if ( $wp_query->max_num_pages < 2 )
261                 return;
262         ?>
263         <nav class="navigation paging-navigation" role="navigation">
264                 <h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentythirteen' ); ?></h1>
265                 <div class="nav-links">
266
267                         <?php if ( get_next_posts_link() ) : ?>
268                         <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentythirteen' ) ); ?></div>
269                         <?php endif; ?>
270
271                         <?php if ( get_previous_posts_link() ) : ?>
272                         <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentythirteen' ) ); ?></div>
273                         <?php endif; ?>
274
275                 </div><!-- .nav-links -->
276         </nav><!-- .navigation -->
277         <?php
278 }
279 endif;
280
281 if ( ! function_exists( 'twentythirteen_post_nav' ) ) :
282 /**
283  * Displays navigation to next/previous post when applicable.
284 *
285 * @since Twenty Thirteen 1.0
286 *
287 * @return void
288 */
289 function twentythirteen_post_nav() {
290         global $post;
291
292         // Don't print empty markup if there's nowhere to navigate.
293         $previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
294         $next     = get_adjacent_post( false, '', false );
295
296         if ( ! $next && ! $previous )
297                 return;
298         ?>
299         <nav class="navigation post-navigation" role="navigation">
300                 <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'twentythirteen' ); ?></h1>
301                 <div class="nav-links">
302
303                         <?php previous_post_link( '%link', _x( '<span class="meta-nav">&larr;</span> %title', 'Previous post link', 'twentythirteen' ) ); ?>
304                         <?php next_post_link( '%link', _x( '%title <span class="meta-nav">&rarr;</span>', 'Next post link', 'twentythirteen' ) ); ?>
305
306                 </div><!-- .nav-links -->
307         </nav><!-- .navigation -->
308         <?php
309 }
310 endif;
311
312 if ( ! function_exists( 'twentythirteen_entry_meta' ) ) :
313 /**
314  * Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
315  *
316  * Create your own twentythirteen_entry_meta() to override in a child theme.
317  *
318  * @since Twenty Thirteen 1.0
319  *
320  * @return void
321  */
322 function twentythirteen_entry_meta() {
323         if ( is_sticky() && is_home() && ! is_paged() )
324                 echo '<span class="featured-post">' . __( 'Sticky', 'twentythirteen' ) . '</span>';
325
326         if ( ! has_post_format( 'link' ) && 'post' == get_post_type() )
327                 twentythirteen_entry_date();
328
329         // Translators: used between list items, there is a space after the comma.
330         $categories_list = get_the_category_list( __( ', ', 'twentythirteen' ) );
331         if ( $categories_list ) {
332                 echo '<span class="categories-links">' . $categories_list . '</span>';
333         }
334
335         // Translators: used between list items, there is a space after the comma.
336         $tag_list = get_the_tag_list( '', __( ', ', 'twentythirteen' ) );
337         if ( $tag_list ) {
338                 echo '<span class="tags-links">' . $tag_list . '</span>';
339         }
340
341         // Post author
342         if ( 'post' == get_post_type() ) {
343                 printf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
344                         esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
345                         esc_attr( sprintf( __( 'View all posts by %s', 'twentythirteen' ), get_the_author() ) ),
346                         get_the_author()
347                 );
348         }
349 }
350 endif;
351
352 if ( ! function_exists( 'twentythirteen_entry_date' ) ) :
353 /**
354  * Prints HTML with date information for current post.
355  *
356  * Create your own twentythirteen_entry_date() to override in a child theme.
357  *
358  * @since Twenty Thirteen 1.0
359  *
360  * @param boolean $echo Whether to echo the date. Default true.
361  * @return string The HTML-formatted post date.
362  */
363 function twentythirteen_entry_date( $echo = true ) {
364         if ( has_post_format( array( 'chat', 'status' ) ) )
365                 $format_prefix = _x( '%1$s on %2$s', '1: post format name. 2: date', 'twentythirteen' );
366         else
367                 $format_prefix = '%2$s';
368
369         $date = sprintf( '<span class="date"><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a></span>',
370                 esc_url( get_permalink() ),
371                 esc_attr( sprintf( __( 'Permalink to %s', 'twentythirteen' ), the_title_attribute( 'echo=0' ) ) ),
372                 esc_attr( get_the_date( 'c' ) ),
373                 esc_html( sprintf( $format_prefix, get_post_format_string( get_post_format() ), get_the_date() ) )
374         );
375
376         if ( $echo )
377                 echo $date;
378
379         return $date;
380 }
381 endif;
382
383 if ( ! function_exists( 'twentythirteen_the_attached_image' ) ) :
384 /**
385  * Prints the attached image with a link to the next attached image.
386  *
387  * @since Twenty Thirteen 1.0
388  *
389  * @return void
390  */
391 function twentythirteen_the_attached_image() {
392         $post                = get_post();
393         $attachment_size     = apply_filters( 'twentythirteen_attachment_size', array( 724, 724 ) );
394         $next_attachment_url = wp_get_attachment_url();
395
396         /**
397          * Grab the IDs of all the image attachments in a gallery so we can get the URL
398          * of the next adjacent image in a gallery, or the first image (if we're
399          * looking at the last image in a gallery), or, in a gallery of one, just the
400          * link to that image file.
401          */
402         $attachment_ids = get_posts( array(
403                 'post_parent'    => $post->post_parent,
404                 'fields'         => 'ids',
405                 'numberposts'    => -1,
406                 'post_status'    => 'inherit',
407                 'post_type'      => 'attachment',
408                 'post_mime_type' => 'image',
409                 'order'          => 'ASC',
410                 'orderby'        => 'menu_order ID'
411         ) );
412
413         // If there is more than 1 attachment in a gallery...
414         if ( count( $attachment_ids ) > 1 ) {
415                 foreach ( $attachment_ids as $attachment_id ) {
416                         if ( $attachment_id == $post->ID ) {
417                                 $next_id = current( $attachment_ids );
418                                 break;
419                         }
420                 }
421
422                 // get the URL of the next image attachment...
423                 if ( $next_id )
424                         $next_attachment_url = get_attachment_link( $next_id );
425
426                 // or get the URL of the first image attachment.
427                 else
428                         $next_attachment_url = get_attachment_link( array_shift( $attachment_ids ) );
429         }
430
431         printf( '<a href="%1$s" title="%2$s" rel="attachment">%3$s</a>',
432                 esc_url( $next_attachment_url ),
433                 the_title_attribute( array( 'echo' => false ) ),
434                 wp_get_attachment_image( $post->ID, $attachment_size )
435         );
436 }
437 endif;
438
439 /**
440  * Returns the URL from the post.
441  *
442  * @uses get_url_in_content() to get the URL in the post meta (if it exists) or
443  * the first link found in the post content.
444  *
445  * Falls back to the post permalink if no URL is found in the post.
446  *
447  * @since Twenty Thirteen 1.0
448  *
449  * @return string The Link format URL.
450  */
451 function twentythirteen_get_link_url() {
452         $content = get_the_content();
453         $has_url = get_url_in_content( $content );
454
455         return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
456 }
457
458 /**
459  * Extends the default WordPress body classes.
460  *
461  * Adds body classes to denote:
462  * 1. Single or multiple authors.
463  * 2. Active widgets in the sidebar to change the layout and spacing.
464  * 3. When avatars are disabled in discussion settings.
465  *
466  * @since Twenty Thirteen 1.0
467  *
468  * @param array $classes A list of existing body class values.
469  * @return array The filtered body class list.
470  */
471 function twentythirteen_body_class( $classes ) {
472         if ( ! is_multi_author() )
473                 $classes[] = 'single-author';
474
475         if ( is_active_sidebar( 'sidebar-2' ) && ! is_attachment() && ! is_404() )
476                 $classes[] = 'sidebar';
477
478         if ( ! get_option( 'show_avatars' ) )
479                 $classes[] = 'no-avatars';
480
481         return $classes;
482 }
483 add_filter( 'body_class', 'twentythirteen_body_class' );
484
485 /**
486  * Adjusts content_width value for video post formats and attachment templates.
487  *
488  * @since Twenty Thirteen 1.0
489  *
490  * @return void
491  */
492 function twentythirteen_content_width() {
493         global $content_width;
494
495         if ( is_attachment() )
496                 $content_width = 724;
497         elseif ( has_post_format( 'audio' ) )
498                 $content_width = 484;
499 }
500 add_action( 'template_redirect', 'twentythirteen_content_width' );
501
502 /**
503  * Add postMessage support for site title and description for the Customizer.
504  *
505  * @since Twenty Thirteen 1.0
506  *
507  * @param WP_Customize_Manager $wp_customize Customizer object.
508  * @return void
509  */
510 function twentythirteen_customize_register( $wp_customize ) {
511         $wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
512         $wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
513         $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
514 }
515 add_action( 'customize_register', 'twentythirteen_customize_register' );
516
517 /**
518  * Binds JavaScript handlers to make Customizer preview reload changes
519  * asynchronously.
520  *
521  * @since Twenty Thirteen 1.0
522  */
523 function twentythirteen_customize_preview_js() {
524         wp_enqueue_script( 'twentythirteen-customizer', get_template_directory_uri() . '/js/theme-customizer.js', array( 'customize-preview' ), '20130226', true );
525 }
526 add_action( 'customize_preview_init', 'twentythirteen_customize_preview_js' );