3 * Twenty Seventeen functions and definitions
5 * @link https://developer.wordpress.org/themes/basics/theme-functions/
8 * @subpackage Twenty_Seventeen
13 * Twenty Seventeen only works in WordPress 4.7 or later.
15 if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '<' ) ) {
16 require get_template_directory() . '/inc/back-compat.php';
21 * Sets up theme defaults and registers support for various WordPress features.
23 * Note that this function is hooked into the after_setup_theme hook, which
24 * runs before the init hook. The init hook is too late for some features, such
25 * as indicating support for post thumbnails.
27 function twentyseventeen_setup() {
29 * Make theme available for translation.
30 * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/twentyseventeen
31 * If you're building a theme based on Twenty Seventeen, use a find and replace
32 * to change 'twentyseventeen' to the name of your theme in all the template files.
34 load_theme_textdomain( 'twentyseventeen' );
36 // Add default posts and comments RSS feed links to head.
37 add_theme_support( 'automatic-feed-links' );
40 * Let WordPress manage the document title.
41 * By adding theme support, we declare that this theme does not use a
42 * hard-coded <title> tag in the document head, and expect WordPress to
45 add_theme_support( 'title-tag' );
48 * Enable support for Post Thumbnails on posts and pages.
50 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
52 add_theme_support( 'post-thumbnails' );
54 add_image_size( 'twentyseventeen-featured-image', 2000, 1200, true );
56 add_image_size( 'twentyseventeen-thumbnail-avatar', 100, 100, true );
58 // This theme uses wp_nav_menu() in two locations.
59 register_nav_menus( array(
60 'top' => __( 'Top Menu', 'twentyseventeen' ),
61 'social' => __( 'Social Links Menu', 'twentyseventeen' ),
65 * Switch default core markup for search form, comment form, and comments
66 * to output valid HTML5.
68 add_theme_support( 'html5', array(
76 * Enable support for Post Formats.
78 * See: https://codex.wordpress.org/Post_Formats
80 add_theme_support( 'post-formats', array(
90 // Add theme support for Custom Logo.
91 add_theme_support( 'custom-logo', array(
97 // Add theme support for selective refresh for widgets.
98 add_theme_support( 'customize-selective-refresh-widgets' );
101 * This theme styles the visual editor to resemble the theme style,
102 * specifically font, colors, and column width.
104 add_editor_style( array( 'assets/css/editor-style.css', twentyseventeen_fonts_url() ) );
106 add_theme_support( 'starter-content', array(
108 'sidebar-1' => array(
109 'text_business_info',
114 'sidebar-2' => array(
115 'text_business_info',
118 'sidebar-3' => array(
127 'thumbnail' => '{{image-sandwich}}',
130 'thumbnail' => '{{image-espresso}}',
133 'thumbnail' => '{{image-coffee}}',
135 'homepage-section' => array(
136 'thumbnail' => '{{image-espresso}}',
140 'attachments' => array(
141 'image-espresso' => array(
142 'post_title' => _x( 'Espresso', 'Theme starter content', 'twentyseventeen' ),
143 'file' => 'assets/images/espresso.jpg',
145 'image-sandwich' => array(
146 'post_title' => _x( 'Sandwich', 'Theme starter content', 'twentyseventeen' ),
147 'file' => 'assets/images/sandwich.jpg',
149 'image-coffee' => array(
150 'post_title' => _x( 'Coffee', 'Theme starter content', 'twentyseventeen' ),
151 'file' => 'assets/images/coffee.jpg',
156 'show_on_front' => 'page',
157 'page_on_front' => '{{home}}',
158 'page_for_posts' => '{{blog}}',
161 'theme_mods' => array(
162 'panel_1' => '{{homepage-section}}',
163 'panel_2' => '{{about}}',
164 'panel_3' => '{{blog}}',
165 'panel_4' => '{{contact}}',
168 'nav_menus' => array(
170 'name' => __( 'Top Menu', 'twentyseventeen' ),
179 'name' => __( 'Social Links Menu', 'twentyseventeen' ),
191 add_action( 'after_setup_theme', 'twentyseventeen_setup' );
194 * Set the content width in pixels, based on the theme's design and stylesheet.
196 * Priority 0 to make it available to lower priority callbacks.
198 * @global int $content_width
200 function twentyseventeen_content_width() {
202 $content_width = 700;
204 if ( twentyseventeen_is_frontpage() ) {
205 $content_width = 1120;
209 * Filter Twenty Seventeen content width of the theme.
211 * @since Twenty Seventeen 1.0
213 * @param $content_width integer
215 $GLOBALS['content_width'] = apply_filters( 'twentyseventeen_content_width', $content_width );
217 add_action( 'after_setup_theme', 'twentyseventeen_content_width', 0 );
220 * Register custom fonts.
222 function twentyseventeen_fonts_url() {
226 * Translators: If there are characters in your language that are not
227 * supported by Libre Franklin, translate this to 'off'. Do not translate
228 * into your own language.
230 $libre_franklin = _x( 'on', 'Libre Franklin font: on or off', 'twentyseventeen' );
232 if ( 'off' !== $libre_franklin ) {
233 $font_families = array();
235 $font_families[] = 'Libre Franklin:300,300i,400,400i,600,600i,800,800i';
238 'family' => urlencode( implode( '|', $font_families ) ),
239 'subset' => urlencode( 'latin,latin-ext' ),
242 $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
245 return esc_url_raw( $fonts_url );
249 * Add preconnect for Google Fonts.
251 * @since Twenty Seventeen 1.0
253 * @param array $urls URLs to print for resource hints.
254 * @param string $relation_type The relation type the URLs are printed.
255 * @return array $urls URLs to print for resource hints.
257 function twentyseventeen_resource_hints( $urls, $relation_type ) {
258 if ( wp_style_is( 'twentyseventeen-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
260 'href' => 'https://fonts.gstatic.com',
267 add_filter( 'wp_resource_hints', 'twentyseventeen_resource_hints', 10, 2 );
270 * Register widget area.
272 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
274 function twentyseventeen_widgets_init() {
275 register_sidebar( array(
276 'name' => __( 'Sidebar', 'twentyseventeen' ),
278 'description' => __( 'Add widgets here to appear in your sidebar.', 'twentyseventeen' ),
279 'before_widget' => '<section id="%1$s" class="widget %2$s">',
280 'after_widget' => '</section>',
281 'before_title' => '<h2 class="widget-title">',
282 'after_title' => '</h2>',
285 register_sidebar( array(
286 'name' => __( 'Footer 1', 'twentyseventeen' ),
288 'description' => __( 'Add widgets here to appear in your footer.', 'twentyseventeen' ),
289 'before_widget' => '<section id="%1$s" class="widget %2$s">',
290 'after_widget' => '</section>',
291 'before_title' => '<h2 class="widget-title">',
292 'after_title' => '</h2>',
295 register_sidebar( array(
296 'name' => __( 'Footer 2', 'twentyseventeen' ),
298 'description' => __( 'Add widgets here to appear in your footer.', 'twentyseventeen' ),
299 'before_widget' => '<section id="%1$s" class="widget %2$s">',
300 'after_widget' => '</section>',
301 'before_title' => '<h2 class="widget-title">',
302 'after_title' => '</h2>',
305 add_action( 'widgets_init', 'twentyseventeen_widgets_init' );
308 * Replaces "[...]" (appended to automatically generated excerpts) with ... and
309 * a 'Continue reading' link.
311 * @since Twenty Seventeen 1.0
313 * @return string 'Continue reading' link prepended with an ellipsis.
315 function twentyseventeen_excerpt_more( $link ) {
320 $link = sprintf( '<p class="link-more"><a href="%1$s" class="more-link">%2$s</a></p>',
321 esc_url( get_permalink( get_the_ID() ) ),
322 /* translators: %s: Name of current post */
323 sprintf( __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentyseventeen' ), get_the_title( get_the_ID() ) )
325 return ' … ' . $link;
327 add_filter( 'excerpt_more', 'twentyseventeen_excerpt_more' );
330 * Handles JavaScript detection.
332 * Adds a `js` class to the root `<html>` element when JavaScript is detected.
334 * @since Twenty Seventeen 1.0
336 function twentyseventeen_javascript_detection() {
337 echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
339 add_action( 'wp_head', 'twentyseventeen_javascript_detection', 0 );
342 * Add a pingback url auto-discovery header for singularly identifiable articles.
344 function twentyseventeen_pingback_header() {
345 if ( is_singular() && pings_open() ) {
346 printf( '<link rel="pingback" href="%s">' . "\n", get_bloginfo( 'pingback_url' ) );
349 add_action( 'wp_head', 'twentyseventeen_pingback_header' );
352 * Display custom color CSS.
354 function twentyseventeen_colors_css_wrap() {
355 if ( 'custom' !== get_theme_mod( 'colorscheme' ) && ! is_customize_preview() ) {
359 require_once( get_parent_theme_file_path( '/inc/color-patterns.php' ) );
360 $hue = absint( get_theme_mod( 'colorscheme_hue', 250 ) );
362 <style type="text/css" id="custom-theme-colors" <?php if ( is_customize_preview() ) { echo 'data-hue="' . $hue . '"'; } ?>>
363 <?php echo twentyseventeen_custom_colors_css(); ?>
366 add_action( 'wp_head', 'twentyseventeen_colors_css_wrap' );
369 * Enqueue scripts and styles.
371 function twentyseventeen_scripts() {
372 // Add custom fonts, used in the main stylesheet.
373 wp_enqueue_style( 'twentyseventeen-fonts', twentyseventeen_fonts_url(), array(), null );
376 wp_enqueue_style( 'twentyseventeen-style', get_stylesheet_uri() );
378 // Load the dark colorscheme.
379 if ( 'dark' === get_theme_mod( 'colorscheme', 'light' ) || is_customize_preview() ) {
380 wp_enqueue_style( 'twentyseventeen-colors-dark', get_theme_file_uri( '/assets/css/colors-dark.css' ), array( 'twentyseventeen-style' ), '1.0' );
383 // Load the Internet Explorer 9 specific stylesheet, to fix display issues in the Customizer.
384 if ( is_customize_preview() ) {
385 wp_enqueue_style( 'twentyseventeen-ie9', get_theme_file_uri( '/assets/css/ie9.css' ), array( 'twentyseventeen-style' ), '1.0' );
386 wp_style_add_data( 'twentyseventeen-ie9', 'conditional', 'IE 9' );
389 // Load the Internet Explorer 8 specific stylesheet.
390 wp_enqueue_style( 'twentyseventeen-ie8', get_theme_file_uri( '/assets/css/ie8.css' ), array( 'twentyseventeen-style' ), '1.0' );
391 wp_style_add_data( 'twentyseventeen-ie8', 'conditional', 'lt IE 9' );
393 // Load the html5 shiv.
394 wp_enqueue_script( 'html5', get_theme_file_uri( '/assets/js/html5.js' ), array(), '3.7.3' );
395 wp_script_add_data( 'html5', 'conditional', 'lt IE 9' );
397 wp_enqueue_script( 'twentyseventeen-skip-link-focus-fix', get_theme_file_uri( '/assets/js/skip-link-focus-fix.js' ), array(), '1.0', true );
399 $twentyseventeen_l10n = array(
400 'quote' => twentyseventeen_get_svg( array( 'icon' => 'quote-right' ) ),
403 if ( has_nav_menu( 'top' ) ) {
404 wp_enqueue_script( 'twentyseventeen-navigation', get_theme_file_uri( '/assets/js/navigation.js' ), array(), '1.0', true );
405 $twentyseventeen_l10n['expand'] = __( 'Expand child menu', 'twentyseventeen' );
406 $twentyseventeen_l10n['collapse'] = __( 'Collapse child menu', 'twentyseventeen' );
407 $twentyseventeen_l10n['icon'] = twentyseventeen_get_svg( array( 'icon' => 'angle-down', 'fallback' => true ) );
410 wp_enqueue_script( 'twentyseventeen-global', get_theme_file_uri( '/assets/js/global.js' ), array( 'jquery' ), '1.0', true );
412 wp_enqueue_script( 'jquery-scrollto', get_theme_file_uri( '/assets/js/jquery.scrollTo.js' ), array( 'jquery' ), '2.1.2', true );
414 wp_localize_script( 'twentyseventeen-skip-link-focus-fix', 'twentyseventeenScreenReaderText', $twentyseventeen_l10n );
416 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
417 wp_enqueue_script( 'comment-reply' );
420 add_action( 'wp_enqueue_scripts', 'twentyseventeen_scripts' );
423 * Add custom image sizes attribute to enhance responsive image functionality
424 * for content images.
426 * @since Twenty Seventeen 1.0
428 * @param string $sizes A source size value for use in a 'sizes' attribute.
429 * @param array $size Image size. Accepts an array of width and height
430 * values in pixels (in that order).
431 * @return string A source size value for use in a content image 'sizes' attribute.
433 function twentyseventeen_content_image_sizes_attr( $sizes, $size ) {
436 if ( 740 <= $width ) {
437 $sizes = '(max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px';
440 if ( is_active_sidebar( 'sidebar-1' ) || is_archive() || is_search() || is_home() || is_page() ) {
441 if ( ! ( is_page() && 'one-column' === get_theme_mod( 'page_options' ) ) && 767 <= $width ) {
442 $sizes = '(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px';
448 add_filter( 'wp_calculate_image_sizes', 'twentyseventeen_content_image_sizes_attr', 10, 2 );
451 * Filter the `sizes` value in the header image markup.
453 * @since Twenty Seventeen 1.0
455 * @param string $html The HTML image tag markup being filtered.
456 * @param object $header The custom header object returned by 'get_custom_header()'.
457 * @param array $attr Array of the attributes for the image tag.
458 * @return string The filtered header image HTML.
460 function twentyseventeen_header_image_tag( $html, $header, $attr ) {
461 if ( isset( $attr['sizes'] ) ) {
462 $html = str_replace( $attr['sizes'], '100vw', $html );
466 add_filter( 'get_header_image_tag', 'twentyseventeen_header_image_tag', 10, 3 );
469 * Add custom image sizes attribute to enhance responsive image functionality
470 * for post thumbnails.
472 * @since Twenty Seventeen 1.0
474 * @param array $attr Attributes for the image markup.
475 * @param int $attachment Image attachment ID.
476 * @param array $size Registered image size or flat array of height and width dimensions.
477 * @return string A source size value for use in a post thumbnail 'sizes' attribute.
479 function twentyseventeen_post_thumbnail_sizes_attr( $attr, $attachment, $size ) {
480 if ( is_archive() || is_search() || is_home() ) {
481 $attr['sizes'] = '(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px';
483 $attr['sizes'] = '100vw';
488 add_filter( 'wp_get_attachment_image_attributes', 'twentyseventeen_post_thumbnail_sizes_attr', 10, 3 );
491 * Use front-page.php when Front page displays is set to a static page.
493 * @since Twenty Seventeen 1.0
495 * @param string $template front-page.php.
497 * @return string The template to be used: blank if is_home() is true (defaults to index.php), else $template.
499 function twentyseventeen_front_page_template( $template ) {
500 return is_home() ? '' : $template;
502 add_filter( 'frontpage_template', 'twentyseventeen_front_page_template' );
505 * Implement the Custom Header feature.
507 require get_parent_theme_file_path( '/inc/custom-header.php' );
510 * Custom template tags for this theme.
512 require get_parent_theme_file_path( '/inc/template-tags.php' );
515 * Additional features to allow styling of the templates.
517 require get_parent_theme_file_path( '/inc/template-functions.php' );
520 * Customizer additions.
522 require get_parent_theme_file_path( '/inc/customizer.php' );
525 * SVG icons functions and filters.
527 require get_parent_theme_file_path( '/inc/icon-functions.php' );