]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/themes/twentysixteen/functions.php
WordPress 4.4
[autoinstalls/wordpress.git] / wp-content / themes / twentysixteen / functions.php
1 <?php
2 /**
3  * Twenty Sixteen 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_Sixteen
25  * @since Twenty Sixteen 1.0
26  */
27
28 /**
29  * Twenty Sixteen only works in WordPress 4.4 or later.
30  */
31 if ( version_compare( $GLOBALS['wp_version'], '4.4-alpha', '<' ) ) {
32         require get_template_directory() . '/inc/back-compat.php';
33 }
34
35 if ( ! function_exists( 'twentysixteen_setup' ) ) :
36 /**
37  * Sets up theme defaults and registers support for various WordPress features.
38  *
39  * Note that this function is hooked into the after_setup_theme hook, which
40  * runs before the init hook. The init hook is too late for some features, such
41  * as indicating support for post thumbnails.
42  *
43  * Create your own twentysixteen_setup() function to override in a child theme.
44  *
45  * @since Twenty Sixteen 1.0
46  */
47 function twentysixteen_setup() {
48         /*
49          * Make theme available for translation.
50          * Translations can be filed in the /languages/ directory.
51          * If you're building a theme based on Twenty Sixteen, use a find and replace
52          * to change 'twentysixteen' to the name of your theme in all the template files
53          */
54         load_theme_textdomain( 'twentysixteen', get_template_directory() . '/languages' );
55
56         // Add default posts and comments RSS feed links to head.
57         add_theme_support( 'automatic-feed-links' );
58
59         /*
60          * Let WordPress manage the document title.
61          * By adding theme support, we declare that this theme does not use a
62          * hard-coded <title> tag in the document head, and expect WordPress to
63          * provide it for us.
64          */
65         add_theme_support( 'title-tag' );
66
67         /*
68          * Enable support for Post Thumbnails on posts and pages.
69          *
70          * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
71          */
72         add_theme_support( 'post-thumbnails' );
73         set_post_thumbnail_size( 1200, 0, true );
74
75         // This theme uses wp_nav_menu() in two locations.
76         register_nav_menus( array(
77                 'primary' => __( 'Primary Menu', 'twentysixteen' ),
78                 'social'  => __( 'Social Links Menu', 'twentysixteen' ),
79         ) );
80
81         /*
82          * Switch default core markup for search form, comment form, and comments
83          * to output valid HTML5.
84          */
85         add_theme_support( 'html5', array(
86                 'search-form',
87                 'comment-form',
88                 'comment-list',
89                 'gallery',
90                 'caption',
91         ) );
92
93         /*
94          * Enable support for Post Formats.
95          *
96          * See: https://codex.wordpress.org/Post_Formats
97          */
98         add_theme_support( 'post-formats', array(
99                 'aside',
100                 'image',
101                 'video',
102                 'quote',
103                 'link',
104                 'gallery',
105                 'status',
106                 'audio',
107                 'chat',
108         ) );
109
110         /*
111          * This theme styles the visual editor to resemble the theme style,
112          * specifically font, colors, icons, and column width.
113          */
114         add_editor_style( array( 'css/editor-style.css', twentysixteen_fonts_url() ) );
115 }
116 endif; // twentysixteen_setup
117 add_action( 'after_setup_theme', 'twentysixteen_setup' );
118
119 /**
120  * Sets the content width in pixels, based on the theme's design and stylesheet.
121  *
122  * Priority 0 to make it available to lower priority callbacks.
123  *
124  * @global int $content_width
125  *
126  * @since Twenty Sixteen 1.0
127  */
128 function twentysixteen_content_width() {
129         $GLOBALS['content_width'] = apply_filters( 'twentysixteen_content_width', 840 );
130 }
131 add_action( 'after_setup_theme', 'twentysixteen_content_width', 0 );
132
133 /**
134  * Registers a widget area.
135  *
136  * @link https://developer.wordpress.org/reference/functions/register_sidebar/
137  *
138  * @since Twenty Sixteen 1.0
139  */
140 function twentysixteen_widgets_init() {
141         register_sidebar( array(
142                 'name'          => __( 'Sidebar', 'twentysixteen' ),
143                 'id'            => 'sidebar-1',
144                 'description'   => __( 'Add widgets here to appear in your sidebar.', 'twentysixteen' ),
145                 'before_widget' => '<section id="%1$s" class="widget %2$s">',
146                 'after_widget'  => '</section>',
147                 'before_title'  => '<h2 class="widget-title">',
148                 'after_title'   => '</h2>',
149         ) );
150
151         register_sidebar( array(
152                 'name'          => __( 'Content Bottom 1', 'twentysixteen' ),
153                 'id'            => 'sidebar-2',
154                 'description'   => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ),
155                 'before_widget' => '<section id="%1$s" class="widget %2$s">',
156                 'after_widget'  => '</section>',
157                 'before_title'  => '<h2 class="widget-title">',
158                 'after_title'   => '</h2>',
159         ) );
160
161         register_sidebar( array(
162                 'name'          => __( 'Content Bottom 2', 'twentysixteen' ),
163                 'id'            => 'sidebar-3',
164                 'description'   => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ),
165                 'before_widget' => '<section id="%1$s" class="widget %2$s">',
166                 'after_widget'  => '</section>',
167                 'before_title'  => '<h2 class="widget-title">',
168                 'after_title'   => '</h2>',
169         ) );
170 }
171 add_action( 'widgets_init', 'twentysixteen_widgets_init' );
172
173 if ( ! function_exists( 'twentysixteen_fonts_url' ) ) :
174 /**
175  * Register Google fonts for Twenty Sixteen.
176  *
177  * Create your own twentysixteen_fonts_url() function to override in a child theme.
178  *
179  * @since Twenty Sixteen 1.0
180  *
181  * @return string Google fonts URL for the theme.
182  */
183 function twentysixteen_fonts_url() {
184         $fonts_url = '';
185         $fonts     = array();
186         $subsets   = 'latin,latin-ext';
187
188         /* translators: If there are characters in your language that are not supported by Merriweather, translate this to 'off'. Do not translate into your own language. */
189         if ( 'off' !== _x( 'on', 'Merriweather font: on or off', 'twentysixteen' ) ) {
190                 $fonts[] = 'Merriweather:400,700,900,400italic,700italic,900italic';
191         }
192
193         /* translators: If there are characters in your language that are not supported by Montserrat, translate this to 'off'. Do not translate into your own language. */
194         if ( 'off' !== _x( 'on', 'Montserrat font: on or off', 'twentysixteen' ) ) {
195                 $fonts[] = 'Montserrat:400,700';
196         }
197
198         /* translators: If there are characters in your language that are not supported by Inconsolata, translate this to 'off'. Do not translate into your own language. */
199         if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentysixteen' ) ) {
200                 $fonts[] = 'Inconsolata:400';
201         }
202
203         if ( $fonts ) {
204                 $fonts_url = add_query_arg( array(
205                         'family' => urlencode( implode( '|', $fonts ) ),
206                         'subset' => urlencode( $subsets ),
207                 ), 'https://fonts.googleapis.com/css' );
208         }
209
210         return $fonts_url;
211 }
212 endif;
213
214 /**
215  * Handles JavaScript detection.
216  *
217  * Adds a `js` class to the root `<html>` element when JavaScript is detected.
218  *
219  * @since Twenty Sixteen 1.0
220  */
221 function twentysixteen_javascript_detection() {
222         echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
223 }
224 add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 );
225
226 /**
227  * Enqueues scripts and styles.
228  *
229  * @since Twenty Sixteen 1.0
230  */
231 function twentysixteen_scripts() {
232         // Add custom fonts, used in the main stylesheet.
233         wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), null );
234
235         // Add Genericons, used in the main stylesheet.
236         wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.4.1' );
237
238         // Theme stylesheet.
239         wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri() );
240
241         // Load the Internet Explorer specific stylesheet.
242         wp_enqueue_style( 'twentysixteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentysixteen-style' ), '20150825' );
243         wp_style_add_data( 'twentysixteen-ie', 'conditional', 'lt IE 10' );
244
245         // Load the Internet Explorer 8 specific stylesheet.
246         wp_enqueue_style( 'twentysixteen-ie8', get_template_directory_uri() . '/css/ie8.css', array( 'twentysixteen-style' ), '20150825' );
247         wp_style_add_data( 'twentysixteen-ie8', 'conditional', 'lt IE 9' );
248
249         // Load the Internet Explorer 7 specific stylesheet.
250         wp_enqueue_style( 'twentysixteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentysixteen-style' ), '20150825' );
251         wp_style_add_data( 'twentysixteen-ie7', 'conditional', 'lt IE 8' );
252
253         // Load the html5 shiv.
254         wp_enqueue_script( 'twentysixteen-html5', get_template_directory_uri() . '/js/html5.js', array(), '3.7.3' );
255         wp_script_add_data( 'twentysixteen-html5', 'conditional', 'lt IE 9' );
256
257         wp_enqueue_script( 'twentysixteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20150825', 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( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20150825' );
265         }
266
267         wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150825', true );
268
269         wp_localize_script( 'twentysixteen-script', 'screenReaderText', array(
270                 'expand'   => __( 'expand child menu', 'twentysixteen' ),
271                 'collapse' => __( 'collapse child menu', 'twentysixteen' ),
272         ) );
273 }
274 add_action( 'wp_enqueue_scripts', 'twentysixteen_scripts' );
275
276 /**
277  * Adds custom classes to the array of body classes.
278  *
279  * @since Twenty Sixteen 1.0
280  *
281  * @param array $classes Classes for the body element.
282  * @return array (Maybe) filtered body classes.
283  */
284 function twentysixteen_body_classes( $classes ) {
285         // Adds a class of custom-background-image to sites with a custom background image.
286         if ( get_background_image() ) {
287                 $classes[] = 'custom-background-image';
288         }
289
290         // Adds a class of group-blog to sites with more than 1 published author.
291         if ( is_multi_author() ) {
292                 $classes[] = 'group-blog';
293         }
294
295         // Adds a class of no-sidebar to sites without active sidebar.
296         if ( ! is_active_sidebar( 'sidebar-1' ) ) {
297                 $classes[] = 'no-sidebar';
298         }
299
300         // Adds a class of hfeed to non-singular pages.
301         if ( ! is_singular() ) {
302                 $classes[] = 'hfeed';
303         }
304
305         return $classes;
306 }
307 add_filter( 'body_class', 'twentysixteen_body_classes' );
308
309 /**
310  * Converts a HEX value to RGB.
311  *
312  * @since Twenty Sixteen 1.0
313  *
314  * @param string $color The original color, in 3- or 6-digit hexadecimal form.
315  * @return array Array containing RGB (red, green, and blue) values for the given
316  *               HEX code, empty array otherwise.
317  */
318 function twentysixteen_hex2rgb( $color ) {
319         $color = trim( $color, '#' );
320
321         if ( strlen( $color ) === 3 ) {
322                 $r = hexdec( substr( $color, 0, 1 ).substr( $color, 0, 1 ) );
323                 $g = hexdec( substr( $color, 1, 1 ).substr( $color, 1, 1 ) );
324                 $b = hexdec( substr( $color, 2, 1 ).substr( $color, 2, 1 ) );
325         } else if ( strlen( $color ) === 6 ) {
326                 $r = hexdec( substr( $color, 0, 2 ) );
327                 $g = hexdec( substr( $color, 2, 2 ) );
328                 $b = hexdec( substr( $color, 4, 2 ) );
329         } else {
330                 return array();
331         }
332
333         return array( 'red' => $r, 'green' => $g, 'blue' => $b );
334 }
335
336 /**
337  * Custom template tags for this theme.
338  */
339 require get_template_directory() . '/inc/template-tags.php';
340
341 /**
342  * Customizer additions.
343  */
344 require get_template_directory() . '/inc/customizer.php';
345
346 /**
347  * Add custom image sizes attribute to enhance responsive image functionality
348  * for content images
349  *
350  * @since Twenty Sixteen 1.0
351  *
352  * @param string $sizes A source size value for use in a 'sizes' attribute.
353  * @param array  $size  Image size. Accepts an array of width and height
354  *                      values in pixels (in that order).
355  * @return string A source size value for use in a content image 'sizes' attribute.
356  */
357 function twentysixteen_content_image_sizes_attr( $sizes, $size ) {
358         $width = $size[0];
359
360         840 <= $width && $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px';
361
362         if ( 'page' === get_post_type() ) {
363                 840 > $width && $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
364         } else {
365                 840 > $width && 600 <= $width && $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px';
366                 600 > $width && $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
367         }
368
369         return $sizes;
370 }
371 add_filter( 'wp_calculate_image_sizes', 'twentysixteen_content_image_sizes_attr', 10 , 2 );
372
373 /**
374  * Add custom image sizes attribute to enhance responsive image functionality
375  * for post thumbnails
376  *
377  * @since Twenty Sixteen 1.0
378  *
379  * @param array $attr Attributes for the image markup.
380  * @param int   $attachment Image attachment ID.
381  * @param array $size Registered image size or flat array of height and width dimensions.
382  * @return string A source size value for use in a post thumbnail 'sizes' attribute.
383  */
384 function twentysixteen_post_thumbnail_sizes_attr( $attr, $attachment, $size ) {
385         if ( 'post-thumbnail' === $size ) {
386                 is_active_sidebar( 'sidebar-1' ) && $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 60vw, (max-width: 1362px) 62vw, 840px';
387                 ! is_active_sidebar( 'sidebar-1' ) && $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 88vw, 1200px';
388         }
389         return $attr;
390 }
391 add_filter( 'wp_get_attachment_image_attributes', 'twentysixteen_post_thumbnail_sizes_attr', 10 , 3 );