]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/themes/twentyfifteen/functions.php
WordPress 4.6.2-scripts
[autoinstalls/wordpress.git] / wp-content / themes / twentyfifteen / functions.php
1 <?php
2 /**
3  * Twenty Fifteen 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 https://codex.wordpress.org/Theme_Development
15  * @link https://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 https://codex.wordpress.org/Plugin_API}
22  *
23  * @package WordPress
24  * @subpackage Twenty_Fifteen
25  * @since Twenty Fifteen 1.0
26  */
27
28 /**
29  * Set the content width based on the theme's design and stylesheet.
30  *
31  * @since Twenty Fifteen 1.0
32  */
33 if ( ! isset( $content_width ) ) {
34         $content_width = 660;
35 }
36
37 /**
38  * Twenty Fifteen only works in WordPress 4.1 or later.
39  */
40 if ( version_compare( $GLOBALS['wp_version'], '4.1-alpha', '<' ) ) {
41         require get_template_directory() . '/inc/back-compat.php';
42 }
43
44 if ( ! function_exists( 'twentyfifteen_setup' ) ) :
45 /**
46  * Sets up theme defaults and registers support for various WordPress features.
47  *
48  * Note that this function is hooked into the after_setup_theme hook, which
49  * runs before the init hook. The init hook is too late for some features, such
50  * as indicating support for post thumbnails.
51  *
52  * @since Twenty Fifteen 1.0
53  */
54 function twentyfifteen_setup() {
55
56         /*
57          * Make theme available for translation.
58          * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/twentyfifteen
59          * If you're building a theme based on twentyfifteen, use a find and replace
60          * to change 'twentyfifteen' to the name of your theme in all the template files
61          */
62         load_theme_textdomain( 'twentyfifteen' );
63
64         // Add default posts and comments RSS feed links to head.
65         add_theme_support( 'automatic-feed-links' );
66
67         /*
68          * Let WordPress manage the document title.
69          * By adding theme support, we declare that this theme does not use a
70          * hard-coded <title> tag in the document head, and expect WordPress to
71          * provide it for us.
72          */
73         add_theme_support( 'title-tag' );
74
75         /*
76          * Enable support for Post Thumbnails on posts and pages.
77          *
78          * See: https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
79          */
80         add_theme_support( 'post-thumbnails' );
81         set_post_thumbnail_size( 825, 510, true );
82
83         // This theme uses wp_nav_menu() in two locations.
84         register_nav_menus( array(
85                 'primary' => __( 'Primary Menu',      'twentyfifteen' ),
86                 'social'  => __( 'Social Links Menu', 'twentyfifteen' ),
87         ) );
88
89         /*
90          * Switch default core markup for search form, comment form, and comments
91          * to output valid HTML5.
92          */
93         add_theme_support( 'html5', array(
94                 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
95         ) );
96
97         /*
98          * Enable support for Post Formats.
99          *
100          * See: https://codex.wordpress.org/Post_Formats
101          */
102         add_theme_support( 'post-formats', array(
103                 'aside', 'image', 'video', 'quote', 'link', 'gallery', 'status', 'audio', 'chat'
104         ) );
105
106         /*
107          * Enable support for custom logo.
108          *
109          * @since Twenty Fifteen 1.5
110          */
111         add_theme_support( 'custom-logo', array(
112                 'height'      => 248,
113                 'width'       => 248,
114                 'flex-height' => true,
115         ) );
116
117         $color_scheme  = twentyfifteen_get_color_scheme();
118         $default_color = trim( $color_scheme[0], '#' );
119
120         // Setup the WordPress core custom background feature.
121         add_theme_support( 'custom-background', apply_filters( 'twentyfifteen_custom_background_args', array(
122                 'default-color'      => $default_color,
123                 'default-attachment' => 'fixed',
124         ) ) );
125
126         /*
127          * This theme styles the visual editor to resemble the theme style,
128          * specifically font, colors, icons, and column width.
129          */
130         add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
131
132         // Indicate widget sidebars can use selective refresh in the Customizer.
133         add_theme_support( 'customize-selective-refresh-widgets' );
134 }
135 endif; // twentyfifteen_setup
136 add_action( 'after_setup_theme', 'twentyfifteen_setup' );
137
138 /**
139  * Register widget area.
140  *
141  * @since Twenty Fifteen 1.0
142  *
143  * @link https://codex.wordpress.org/Function_Reference/register_sidebar
144  */
145 function twentyfifteen_widgets_init() {
146         register_sidebar( array(
147                 'name'          => __( 'Widget Area', 'twentyfifteen' ),
148                 'id'            => 'sidebar-1',
149                 'description'   => __( 'Add widgets here to appear in your sidebar.', 'twentyfifteen' ),
150                 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
151                 'after_widget'  => '</aside>',
152                 'before_title'  => '<h2 class="widget-title">',
153                 'after_title'   => '</h2>',
154         ) );
155 }
156 add_action( 'widgets_init', 'twentyfifteen_widgets_init' );
157
158 if ( ! function_exists( 'twentyfifteen_fonts_url' ) ) :
159 /**
160  * Register Google fonts for Twenty Fifteen.
161  *
162  * @since Twenty Fifteen 1.0
163  *
164  * @return string Google fonts URL for the theme.
165  */
166 function twentyfifteen_fonts_url() {
167         $fonts_url = '';
168         $fonts     = array();
169         $subsets   = 'latin,latin-ext';
170
171         /*
172          * Translators: If there are characters in your language that are not supported
173          * by Noto Sans, translate this to 'off'. Do not translate into your own language.
174          */
175         if ( 'off' !== _x( 'on', 'Noto Sans font: on or off', 'twentyfifteen' ) ) {
176                 $fonts[] = 'Noto Sans:400italic,700italic,400,700';
177         }
178
179         /*
180          * Translators: If there are characters in your language that are not supported
181          * by Noto Serif, translate this to 'off'. Do not translate into your own language.
182          */
183         if ( 'off' !== _x( 'on', 'Noto Serif font: on or off', 'twentyfifteen' ) ) {
184                 $fonts[] = 'Noto Serif:400italic,700italic,400,700';
185         }
186
187         /*
188          * Translators: If there are characters in your language that are not supported
189          * by Inconsolata, translate this to 'off'. Do not translate into your own language.
190          */
191         if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentyfifteen' ) ) {
192                 $fonts[] = 'Inconsolata:400,700';
193         }
194
195         /*
196          * Translators: To add an additional character subset specific to your language,
197          * translate this to 'greek', 'cyrillic', 'devanagari' or 'vietnamese'. Do not translate into your own language.
198          */
199         $subset = _x( 'no-subset', 'Add new subset (greek, cyrillic, devanagari, vietnamese)', 'twentyfifteen' );
200
201         if ( 'cyrillic' == $subset ) {
202                 $subsets .= ',cyrillic,cyrillic-ext';
203         } elseif ( 'greek' == $subset ) {
204                 $subsets .= ',greek,greek-ext';
205         } elseif ( 'devanagari' == $subset ) {
206                 $subsets .= ',devanagari';
207         } elseif ( 'vietnamese' == $subset ) {
208                 $subsets .= ',vietnamese';
209         }
210
211         if ( $fonts ) {
212                 $fonts_url = add_query_arg( array(
213                         'family' => urlencode( implode( '|', $fonts ) ),
214                         'subset' => urlencode( $subsets ),
215                 ), 'https://fonts.googleapis.com/css' );
216         }
217
218         return $fonts_url;
219 }
220 endif;
221
222 /**
223  * JavaScript Detection.
224  *
225  * Adds a `js` class to the root `<html>` element when JavaScript is detected.
226  *
227  * @since Twenty Fifteen 1.1
228  */
229 function twentyfifteen_javascript_detection() {
230         echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
231 }
232 add_action( 'wp_head', 'twentyfifteen_javascript_detection', 0 );
233
234 /**
235  * Enqueue scripts and styles.
236  *
237  * @since Twenty Fifteen 1.0
238  */
239 function twentyfifteen_scripts() {
240         // Add custom fonts, used in the main stylesheet.
241         wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
242
243         // Add Genericons, used in the main stylesheet.
244         wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
245
246         // Load our main stylesheet.
247         wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );
248
249         // Load the Internet Explorer specific stylesheet.
250         wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20141010' );
251         wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );
252
253         // Load the Internet Explorer 7 specific stylesheet.
254         wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
255         wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
256
257         wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );
258
259         if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
260                 wp_enqueue_script( 'comment-reply' );
261         }
262
263         if ( is_singular() && wp_attachment_is_image() ) {
264                 wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
265         }
266
267         wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150330', true );
268         wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
269                 'expand'   => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
270                 'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
271         ) );
272 }
273 add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
274
275 /**
276  * Add featured image as background image to post navigation elements.
277  *
278  * @since Twenty Fifteen 1.0
279  *
280  * @see wp_add_inline_style()
281  */
282 function twentyfifteen_post_nav_background() {
283         if ( ! is_single() ) {
284                 return;
285         }
286
287         $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
288         $next     = get_adjacent_post( false, '', false );
289         $css      = '';
290
291         if ( is_attachment() && 'attachment' == $previous->post_type ) {
292                 return;
293         }
294
295         if ( $previous &&  has_post_thumbnail( $previous->ID ) ) {
296                 $prevthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'post-thumbnail' );
297                 $css .= '
298                         .post-navigation .nav-previous { background-image: url(' . esc_url( $prevthumb[0] ) . '); }
299                         .post-navigation .nav-previous .post-title, .post-navigation .nav-previous a:hover .post-title, .post-navigation .nav-previous .meta-nav { color: #fff; }
300                         .post-navigation .nav-previous a:before { background-color: rgba(0, 0, 0, 0.4); }
301                 ';
302         }
303
304         if ( $next && has_post_thumbnail( $next->ID ) ) {
305                 $nextthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'post-thumbnail' );
306                 $css .= '
307                         .post-navigation .nav-next { background-image: url(' . esc_url( $nextthumb[0] ) . '); border-top: 0; }
308                         .post-navigation .nav-next .post-title, .post-navigation .nav-next a:hover .post-title, .post-navigation .nav-next .meta-nav { color: #fff; }
309                         .post-navigation .nav-next a:before { background-color: rgba(0, 0, 0, 0.4); }
310                 ';
311         }
312
313         wp_add_inline_style( 'twentyfifteen-style', $css );
314 }
315 add_action( 'wp_enqueue_scripts', 'twentyfifteen_post_nav_background' );
316
317 /**
318  * Display descriptions in main navigation.
319  *
320  * @since Twenty Fifteen 1.0
321  *
322  * @param string  $item_output The menu item output.
323  * @param WP_Post $item        Menu item object.
324  * @param int     $depth       Depth of the menu.
325  * @param array   $args        wp_nav_menu() arguments.
326  * @return string Menu item with possible description.
327  */
328 function twentyfifteen_nav_description( $item_output, $item, $depth, $args ) {
329         if ( 'primary' == $args->theme_location && $item->description ) {
330                 $item_output = str_replace( $args->link_after . '</a>', '<div class="menu-item-description">' . $item->description . '</div>' . $args->link_after . '</a>', $item_output );
331         }
332
333         return $item_output;
334 }
335 add_filter( 'walker_nav_menu_start_el', 'twentyfifteen_nav_description', 10, 4 );
336
337 /**
338  * Add a `screen-reader-text` class to the search form's submit button.
339  *
340  * @since Twenty Fifteen 1.0
341  *
342  * @param string $html Search form HTML.
343  * @return string Modified search form HTML.
344  */
345 function twentyfifteen_search_form_modify( $html ) {
346         return str_replace( 'class="search-submit"', 'class="search-submit screen-reader-text"', $html );
347 }
348 add_filter( 'get_search_form', 'twentyfifteen_search_form_modify' );
349
350 /**
351  * Implement the Custom Header feature.
352  *
353  * @since Twenty Fifteen 1.0
354  */
355 require get_template_directory() . '/inc/custom-header.php';
356
357 /**
358  * Custom template tags for this theme.
359  *
360  * @since Twenty Fifteen 1.0
361  */
362 require get_template_directory() . '/inc/template-tags.php';
363
364 /**
365  * Customizer additions.
366  *
367  * @since Twenty Fifteen 1.0
368  */
369 require get_template_directory() . '/inc/customizer.php';