3 * Twenty Sixteen 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_Sixteen
25 * @since Twenty Sixteen 1.0
29 * Twenty Sixteen only works in WordPress 4.4 or later.
31 if ( version_compare( $GLOBALS['wp_version'], '4.4-alpha', '<' ) ) {
32 require get_template_directory() . '/inc/back-compat.php';
35 if ( ! function_exists( 'twentysixteen_setup' ) ) :
37 * Sets up theme defaults and registers support for various WordPress features.
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.
43 * Create your own twentysixteen_setup() function to override in a child theme.
45 * @since Twenty Sixteen 1.0
47 function twentysixteen_setup() {
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
54 load_theme_textdomain( 'twentysixteen', get_template_directory() . '/languages' );
56 // Add default posts and comments RSS feed links to head.
57 add_theme_support( 'automatic-feed-links' );
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
65 add_theme_support( 'title-tag' );
68 * Enable support for Post Thumbnails on posts and pages.
70 * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
72 add_theme_support( 'post-thumbnails' );
73 set_post_thumbnail_size( 1200, 0, true );
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' ),
82 * Switch default core markup for search form, comment form, and comments
83 * to output valid HTML5.
85 add_theme_support( 'html5', array(
94 * Enable support for Post Formats.
96 * See: https://codex.wordpress.org/Post_Formats
98 add_theme_support( 'post-formats', array(
111 * This theme styles the visual editor to resemble the theme style,
112 * specifically font, colors, icons, and column width.
114 add_editor_style( array( 'css/editor-style.css', twentysixteen_fonts_url() ) );
116 endif; // twentysixteen_setup
117 add_action( 'after_setup_theme', 'twentysixteen_setup' );
120 * Sets the content width in pixels, based on the theme's design and stylesheet.
122 * Priority 0 to make it available to lower priority callbacks.
124 * @global int $content_width
126 * @since Twenty Sixteen 1.0
128 function twentysixteen_content_width() {
129 $GLOBALS['content_width'] = apply_filters( 'twentysixteen_content_width', 840 );
131 add_action( 'after_setup_theme', 'twentysixteen_content_width', 0 );
134 * Registers a widget area.
136 * @link https://developer.wordpress.org/reference/functions/register_sidebar/
138 * @since Twenty Sixteen 1.0
140 function twentysixteen_widgets_init() {
141 register_sidebar( array(
142 'name' => __( 'Sidebar', 'twentysixteen' ),
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>',
151 register_sidebar( array(
152 'name' => __( 'Content Bottom 1', 'twentysixteen' ),
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>',
161 register_sidebar( array(
162 'name' => __( 'Content Bottom 2', 'twentysixteen' ),
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>',
171 add_action( 'widgets_init', 'twentysixteen_widgets_init' );
173 if ( ! function_exists( 'twentysixteen_fonts_url' ) ) :
175 * Register Google fonts for Twenty Sixteen.
177 * Create your own twentysixteen_fonts_url() function to override in a child theme.
179 * @since Twenty Sixteen 1.0
181 * @return string Google fonts URL for the theme.
183 function twentysixteen_fonts_url() {
186 $subsets = 'latin,latin-ext';
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';
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';
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';
204 $fonts_url = add_query_arg( array(
205 'family' => urlencode( implode( '|', $fonts ) ),
206 'subset' => urlencode( $subsets ),
207 ), 'https://fonts.googleapis.com/css' );
215 * Handles JavaScript detection.
217 * Adds a `js` class to the root `<html>` element when JavaScript is detected.
219 * @since Twenty Sixteen 1.0
221 function twentysixteen_javascript_detection() {
222 echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
224 add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 );
227 * Enqueues scripts and styles.
229 * @since Twenty Sixteen 1.0
231 function twentysixteen_scripts() {
232 // Add custom fonts, used in the main stylesheet.
233 wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), null );
235 // Add Genericons, used in the main stylesheet.
236 wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.4.1' );
239 wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri() );
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' );
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' );
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' );
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' );
257 wp_enqueue_script( 'twentysixteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20150825', true );
259 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
260 wp_enqueue_script( 'comment-reply' );
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' );
267 wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150825', true );
269 wp_localize_script( 'twentysixteen-script', 'screenReaderText', array(
270 'expand' => __( 'expand child menu', 'twentysixteen' ),
271 'collapse' => __( 'collapse child menu', 'twentysixteen' ),
274 add_action( 'wp_enqueue_scripts', 'twentysixteen_scripts' );
277 * Adds custom classes to the array of body classes.
279 * @since Twenty Sixteen 1.0
281 * @param array $classes Classes for the body element.
282 * @return array (Maybe) filtered body classes.
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';
290 // Adds a class of group-blog to sites with more than 1 published author.
291 if ( is_multi_author() ) {
292 $classes[] = 'group-blog';
295 // Adds a class of no-sidebar to sites without active sidebar.
296 if ( ! is_active_sidebar( 'sidebar-1' ) ) {
297 $classes[] = 'no-sidebar';
300 // Adds a class of hfeed to non-singular pages.
301 if ( ! is_singular() ) {
302 $classes[] = 'hfeed';
307 add_filter( 'body_class', 'twentysixteen_body_classes' );
310 * Converts a HEX value to RGB.
312 * @since Twenty Sixteen 1.0
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.
318 function twentysixteen_hex2rgb( $color ) {
319 $color = trim( $color, '#' );
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 ) );
333 return array( 'red' => $r, 'green' => $g, 'blue' => $b );
337 * Custom template tags for this theme.
339 require get_template_directory() . '/inc/template-tags.php';
342 * Customizer additions.
344 require get_template_directory() . '/inc/customizer.php';
347 * Add custom image sizes attribute to enhance responsive image functionality
350 * @since Twenty Sixteen 1.0
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.
357 function twentysixteen_content_image_sizes_attr( $sizes, $size ) {
360 840 <= $width && $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px';
362 if ( 'page' === get_post_type() ) {
363 840 > $width && $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
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';
371 add_filter( 'wp_calculate_image_sizes', 'twentysixteen_content_image_sizes_attr', 10 , 2 );
374 * Add custom image sizes attribute to enhance responsive image functionality
375 * for post thumbnails
377 * @since Twenty Sixteen 1.0
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.
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';
391 add_filter( 'wp_get_attachment_image_attributes', 'twentysixteen_post_thumbnail_sizes_attr', 10 , 3 );