| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Twenty Sixteen 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 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://developer.wordpress.org/themes/basics/theme-functions/ 15 * @link https://developer.wordpress.org/themes/advanced-topics/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://developer.wordpress.org/plugins/} 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 * @global string $wp_version The WordPress version string. 32 */ 33 if ( version_compare( $GLOBALS['wp_version'], '4.4-alpha', '<' ) ) { 34 require get_template_directory() . '/inc/back-compat.php'; 35 } 36 37 if ( ! function_exists( 'twentysixteen_setup' ) ) : 38 /** 39 * Sets up theme defaults and registers support for various WordPress features. 40 * 41 * Note that this function is hooked into the after_setup_theme hook, which 42 * runs before the init hook. The init hook is too late for some features, such 43 * as indicating support for post thumbnails. 44 * 45 * Create your own twentysixteen_setup() function to override in a child theme. 46 * 47 * @since Twenty Sixteen 1.0 48 * 49 * @global string $wp_version The WordPress version string. 50 */ 51 function twentysixteen_setup() { 52 /* 53 * Make theme available for translation. 54 * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/twentysixteen 55 * If you're building a theme based on Twenty Sixteen, use a find and replace 56 * to change 'twentysixteen' to the name of your theme in all the template files. 57 * 58 * Manual loading of text domain is not required after the introduction of 59 * just in time translation loading in WordPress version 4.6. 60 * 61 * @ticket 58318 62 */ 63 if ( version_compare( $GLOBALS['wp_version'], '4.6', '<' ) ) { 64 load_theme_textdomain( 'twentysixteen' ); 65 } 66 67 // Add default posts and comments RSS feed links to head. 68 add_theme_support( 'automatic-feed-links' ); 69 70 /* 71 * Let WordPress manage the document title. 72 * By adding theme support, we declare that this theme does not use a 73 * hard-coded <title> tag in the document head, and expect WordPress to 74 * provide it for us. 75 */ 76 add_theme_support( 'title-tag' ); 77 78 /* 79 * Enable support for custom logo. 80 * 81 * @since Twenty Sixteen 1.2 82 */ 83 add_theme_support( 84 'custom-logo', 85 array( 86 'height' => 240, 87 'width' => 240, 88 'flex-height' => true, 89 ) 90 ); 91 92 /* 93 * Enable support for Post Thumbnails on posts and pages. 94 * 95 * @link https://developer.wordpress.org/reference/functions/add_theme_support/#post-thumbnails 96 */ 97 add_theme_support( 'post-thumbnails' ); 98 set_post_thumbnail_size( 1200, 9999 ); 99 100 // This theme uses wp_nav_menu() in two locations. 101 register_nav_menus( 102 array( 103 'primary' => __( 'Primary Menu', 'twentysixteen' ), 104 'social' => __( 'Social Links Menu', 'twentysixteen' ), 105 ) 106 ); 107 108 /* 109 * Switch default core markup for search form, comment form, and comments 110 * to output valid HTML5. 111 */ 112 add_theme_support( 113 'html5', 114 array( 115 'search-form', 116 'comment-form', 117 'comment-list', 118 'gallery', 119 'caption', 120 'script', 121 'style', 122 'navigation-widgets', 123 ) 124 ); 125 126 /* 127 * Enable support for Post Formats. 128 * 129 * See: https://developer.wordpress.org/advanced-administration/wordpress/post-formats/ 130 */ 131 add_theme_support( 132 'post-formats', 133 array( 134 'aside', 135 'image', 136 'video', 137 'quote', 138 'link', 139 'gallery', 140 'status', 141 'audio', 142 'chat', 143 ) 144 ); 145 146 /* 147 * This theme styles the visual editor to resemble the theme style, 148 * specifically font, colors, icons, and column width. When fonts are 149 * self-hosted, the theme directory needs to be removed first. 150 */ 151 $font_stylesheet = str_replace( 152 array( get_template_directory_uri() . '/', get_stylesheet_directory_uri() . '/' ), 153 '', 154 (string) twentysixteen_fonts_url() 155 ); 156 add_editor_style( array( 'css/editor-style.css', $font_stylesheet ) ); 157 158 // Load regular editor styles into the new block-based editor. 159 add_theme_support( 'editor-styles' ); 160 161 // Load default block styles. 162 add_theme_support( 'wp-block-styles' ); 163 164 // Add support for responsive embeds. 165 add_theme_support( 'responsive-embeds' ); 166 167 // Add support for custom color scheme. 168 add_theme_support( 169 'editor-color-palette', 170 array( 171 array( 172 'name' => __( 'Dark Gray', 'twentysixteen' ), 173 'slug' => 'dark-gray', 174 'color' => '#1a1a1a', 175 ), 176 array( 177 'name' => __( 'Medium Gray', 'twentysixteen' ), 178 'slug' => 'medium-gray', 179 'color' => '#686868', 180 ), 181 array( 182 'name' => __( 'Light Gray', 'twentysixteen' ), 183 'slug' => 'light-gray', 184 'color' => '#e5e5e5', 185 ), 186 array( 187 'name' => __( 'White', 'twentysixteen' ), 188 'slug' => 'white', 189 'color' => '#fff', 190 ), 191 array( 192 'name' => __( 'Blue Gray', 'twentysixteen' ), 193 'slug' => 'blue-gray', 194 'color' => '#4d545c', 195 ), 196 array( 197 'name' => __( 'Bright Blue', 'twentysixteen' ), 198 'slug' => 'bright-blue', 199 'color' => '#007acc', 200 ), 201 array( 202 'name' => __( 'Light Blue', 'twentysixteen' ), 203 'slug' => 'light-blue', 204 'color' => '#9adffd', 205 ), 206 array( 207 'name' => __( 'Dark Brown', 'twentysixteen' ), 208 'slug' => 'dark-brown', 209 'color' => '#402b30', 210 ), 211 array( 212 'name' => __( 'Medium Brown', 'twentysixteen' ), 213 'slug' => 'medium-brown', 214 'color' => '#774e24', 215 ), 216 array( 217 'name' => __( 'Dark Red', 'twentysixteen' ), 218 'slug' => 'dark-red', 219 'color' => '#640c1f', 220 ), 221 array( 222 'name' => __( 'Bright Red', 'twentysixteen' ), 223 'slug' => 'bright-red', 224 'color' => '#ff675f', 225 ), 226 array( 227 'name' => __( 'Yellow', 'twentysixteen' ), 228 'slug' => 'yellow', 229 'color' => '#ffef8e', 230 ), 231 ) 232 ); 233 234 // Indicate widget sidebars can use selective refresh in the Customizer. 235 add_theme_support( 'customize-selective-refresh-widgets' ); 236 237 // Add support for custom line height controls. 238 add_theme_support( 'custom-line-height' ); 239 } 240 endif; // twentysixteen_setup() 241 add_action( 'after_setup_theme', 'twentysixteen_setup' ); 242 243 /** 244 * Sets the content width in pixels, based on the theme's design and stylesheet. 245 * 246 * Priority 0 to make it available to lower priority callbacks. 247 * 248 * @since Twenty Sixteen 1.0 249 * 250 * @global int $content_width Content width. 251 */ 252 function twentysixteen_content_width() { 253 /** 254 * Filters Twenty Sixteen content width of the theme. 255 * 256 * @since Twenty Sixteen 1.0 257 * 258 * @param int $content_width Content width in pixels. 259 */ 260 $GLOBALS['content_width'] = apply_filters( 'twentysixteen_content_width', 840 ); 261 } 262 add_action( 'after_setup_theme', 'twentysixteen_content_width', 0 ); 263 264 /** 265 * Adds preconnect for Google Fonts. 266 * 267 * @since Twenty Sixteen 1.6 268 * @deprecated Twenty Sixteen 2.9 Disabled filter because, by default, fonts are self-hosted. 269 * 270 * @param array $urls URLs to print for resource hints. 271 * @param string $relation_type The relation type the URLs are printed. 272 * @return array URLs to print for resource hints. 273 */ 274 function twentysixteen_resource_hints( $urls, $relation_type ) { 275 if ( wp_style_is( 'twentysixteen-fonts', 'queue' ) && 'preconnect' === $relation_type ) { 276 $urls[] = array( 277 'href' => 'https://fonts.gstatic.com', 278 'crossorigin', 279 ); 280 } 281 282 return $urls; 283 } 284 // add_filter( 'wp_resource_hints', 'twentysixteen_resource_hints', 10, 2 ); 285 286 /** 287 * Registers a widget area. 288 * 289 * @link https://developer.wordpress.org/reference/functions/register_sidebar/ 290 * 291 * @since Twenty Sixteen 1.0 292 */ 293 function twentysixteen_widgets_init() { 294 register_sidebar( 295 array( 296 'name' => __( 'Sidebar', 'twentysixteen' ), 297 'id' => 'sidebar-1', 298 'description' => __( 'Add widgets here to appear in your sidebar.', 'twentysixteen' ), 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>', 303 ) 304 ); 305 306 register_sidebar( 307 array( 308 'name' => __( 'Content Bottom 1', 'twentysixteen' ), 309 'id' => 'sidebar-2', 310 'description' => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ), 311 'before_widget' => '<section id="%1$s" class="widget %2$s">', 312 'after_widget' => '</section>', 313 'before_title' => '<h2 class="widget-title">', 314 'after_title' => '</h2>', 315 ) 316 ); 317 318 register_sidebar( 319 array( 320 'name' => __( 'Content Bottom 2', 'twentysixteen' ), 321 'id' => 'sidebar-3', 322 'description' => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ), 323 'before_widget' => '<section id="%1$s" class="widget %2$s">', 324 'after_widget' => '</section>', 325 'before_title' => '<h2 class="widget-title">', 326 'after_title' => '</h2>', 327 ) 328 ); 329 } 330 add_action( 'widgets_init', 'twentysixteen_widgets_init' ); 331 332 if ( ! function_exists( 'twentysixteen_fonts_url' ) ) : 333 /** 334 * Registers fonts for Twenty Sixteen. 335 * 336 * Create your own twentysixteen_fonts_url() function to override in a child theme. 337 * 338 * @since Twenty Sixteen 1.0 339 * @since Twenty Sixteen 2.9 Replaced Google URL with self-hosted fonts. 340 * 341 * @return string Font stylesheet URL or empty string if disabled. 342 */ 343 function twentysixteen_fonts_url() { 344 $fonts_url = ''; 345 $fonts = array(); 346 347 /* 348 * translators: If there are characters in your language that are not supported 349 * by Merriweather, translate this to 'off'. Do not translate into your own language. 350 */ 351 if ( 'off' !== _x( 'on', 'Merriweather font: on or off', 'twentysixteen' ) ) { 352 $fonts[] = 'merriweather'; 353 } 354 355 /* 356 * translators: If there are characters in your language that are not supported 357 * by Montserrat, translate this to 'off'. Do not translate into your own language. 358 */ 359 if ( 'off' !== _x( 'on', 'Montserrat font: on or off', 'twentysixteen' ) ) { 360 $fonts[] = 'montserrat'; 361 } 362 363 /* 364 * translators: If there are characters in your language that are not supported 365 * by Inconsolata, translate this to 'off'. Do not translate into your own language. 366 */ 367 if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentysixteen' ) ) { 368 $fonts[] = 'inconsolata'; 369 } 370 371 if ( $fonts ) { 372 $fonts_url = get_template_directory_uri() . '/fonts/' . implode( '-plus-', $fonts ) . '.css'; 373 } 374 375 return $fonts_url; 376 } 377 endif; 378 379 /** 380 * Handles JavaScript detection. 381 * 382 * Adds a `js` class to the root `<html>` element when JavaScript is detected. 383 * 384 * @since Twenty Sixteen 1.0 385 */ 386 function twentysixteen_javascript_detection() { 387 $js = "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);"; 388 $js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ ); 389 390 if ( function_exists( 'wp_print_inline_script_tag' ) ) { 391 wp_print_inline_script_tag( $js ); 392 } else { 393 echo "<script>$js</script>\n"; 394 } 395 } 396 add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 ); 397 398 /** 399 * Enqueues scripts and styles. 400 * 401 * @since Twenty Sixteen 1.0 402 */ 403 function twentysixteen_scripts() { 404 // Add custom fonts, used in the main stylesheet. 405 $font_version = ( 0 === strpos( (string) twentysixteen_fonts_url(), get_template_directory_uri() . '/' ) ) ? '20230328' : null; 406 wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), $font_version ); 407 408 // Add Genericons, used in the main stylesheet. 409 wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '20251101' ); 410 411 // Theme stylesheet. 412 wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri(), array(), '20260520' ); 413 414 // Theme block stylesheet. 415 wp_enqueue_style( 'twentysixteen-block-style', get_template_directory_uri() . '/css/blocks.css', array( 'twentysixteen-style' ), '20260105' ); 416 417 // Register handles for removed stylesheets and scripts. 418 wp_register_style( 'twentysixteen-ie', false, array( 'twentysixteen-style' ) ); 419 wp_register_style( 'twentysixteen-ie8', false, array( 'twentysixteen-style' ) ); 420 wp_register_style( 'twentysixteen-ie7', false, array( 'twentysixteen-style' ) ); 421 wp_register_script( 'twentysixteen-html5', false ); 422 wp_register_script( 'twentysixteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20230526', array( 'in_footer' => true ) ); 423 424 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { 425 wp_enqueue_script( 'comment-reply' ); 426 } 427 428 if ( is_singular() && wp_attachment_is_image() ) { 429 wp_enqueue_script( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20170530' ); 430 } 431 432 wp_enqueue_script( 433 'twentysixteen-script', 434 get_template_directory_uri() . '/js/functions.js', 435 array( 'jquery' ), 436 '20230629', 437 array( 438 'in_footer' => false, // Because involves header. 439 'strategy' => 'defer', 440 ) 441 ); 442 443 wp_localize_script( 444 'twentysixteen-script', 445 'screenReaderText', 446 array( 447 'expand' => __( 'expand child menu', 'twentysixteen' ), 448 'collapse' => __( 'collapse child menu', 'twentysixteen' ), 449 ) 450 ); 451 } 452 add_action( 'wp_enqueue_scripts', 'twentysixteen_scripts' ); 453 454 /** 455 * Enqueues styles for the block-based editor. 456 * 457 * @since Twenty Sixteen 1.6 458 */ 459 function twentysixteen_block_editor_styles() { 460 // Block styles. 461 wp_enqueue_style( 'twentysixteen-block-editor-style', get_template_directory_uri() . '/css/editor-blocks.css', array(), '20260105' ); 462 // Add custom fonts. 463 $font_version = ( 0 === strpos( (string) twentysixteen_fonts_url(), get_template_directory_uri() . '/' ) ) ? '20230328' : null; 464 wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), $font_version ); 465 } 466 add_action( 'enqueue_block_editor_assets', 'twentysixteen_block_editor_styles' ); 467 468 /** 469 * Adds custom classes to the array of body classes. 470 * 471 * @since Twenty Sixteen 1.0 472 * 473 * @param array $classes Classes for the body element. 474 * @return array (Maybe) filtered body classes. 475 */ 476 function twentysixteen_body_classes( $classes ) { 477 // Adds a class of custom-background-image to sites with a custom background image. 478 if ( get_background_image() ) { 479 $classes[] = 'custom-background-image'; 480 } 481 482 // Adds a class of group-blog to sites with more than 1 published author. 483 if ( is_multi_author() ) { 484 $classes[] = 'group-blog'; 485 } 486 487 // Adds a class of no-sidebar to sites without active sidebar. 488 if ( ! is_active_sidebar( 'sidebar-1' ) ) { 489 $classes[] = 'no-sidebar'; 490 } 491 492 // Adds a class of hfeed to non-singular pages. 493 if ( ! is_singular() ) { 494 $classes[] = 'hfeed'; 495 } 496 497 return $classes; 498 } 499 add_filter( 'body_class', 'twentysixteen_body_classes' ); 500 501 /** 502 * Converts a HEX value to RGB. 503 * 504 * @since Twenty Sixteen 1.0 505 * 506 * @param string $color The original color, in 3- or 6-digit hexadecimal form. 507 * @return array Array containing RGB (red, green, and blue) values for the given 508 * HEX code, empty array otherwise. 509 */ 510 function twentysixteen_hex2rgb( $color ) { 511 $color = trim( $color, '#' ); 512 513 if ( strlen( $color ) === 3 ) { 514 $r = hexdec( substr( $color, 0, 1 ) . substr( $color, 0, 1 ) ); 515 $g = hexdec( substr( $color, 1, 1 ) . substr( $color, 1, 1 ) ); 516 $b = hexdec( substr( $color, 2, 1 ) . substr( $color, 2, 1 ) ); 517 } elseif ( strlen( $color ) === 6 ) { 518 $r = hexdec( substr( $color, 0, 2 ) ); 519 $g = hexdec( substr( $color, 2, 2 ) ); 520 $b = hexdec( substr( $color, 4, 2 ) ); 521 } else { 522 return array(); 523 } 524 525 return array( 526 'red' => $r, 527 'green' => $g, 528 'blue' => $b, 529 ); 530 } 531 532 /** 533 * Custom template tags for this theme. 534 */ 535 require get_template_directory() . '/inc/template-tags.php'; 536 537 538 /** 539 * Registers block patterns and pattern categories. 540 * 541 * @since Twenty Sixteen 3.4 542 */ 543 function twentysixteen_register_block_patterns() { 544 require get_template_directory() . '/inc/block-patterns.php'; 545 } 546 547 add_action( 'init', 'twentysixteen_register_block_patterns' ); 548 549 /** 550 * Customizer additions. 551 */ 552 require get_template_directory() . '/inc/customizer.php'; 553 554 /** 555 * Adds custom image sizes attribute to enhance responsive image functionality 556 * for content images 557 * 558 * @since Twenty Sixteen 1.0 559 * 560 * @param string $sizes A source size value for use in a 'sizes' attribute. 561 * @param array $size Image size. Accepts an array of width and height 562 * values in pixels (in that order). 563 * @return string A source size value for use in a content image 'sizes' attribute. 564 */ 565 function twentysixteen_content_image_sizes_attr( $sizes, $size ) { 566 $width = $size[0]; 567 568 if ( 840 <= $width ) { 569 $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px'; 570 } 571 572 if ( 'page' === get_post_type() ) { 573 if ( 840 > $width ) { 574 $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px'; 575 } 576 } else { 577 if ( 840 > $width && 600 <= $width ) { 578 $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px'; 579 } elseif ( 600 > $width ) { 580 $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px'; 581 } 582 } 583 584 return $sizes; 585 } 586 add_filter( 'wp_calculate_image_sizes', 'twentysixteen_content_image_sizes_attr', 10, 2 ); 587 588 /** 589 * Adds custom image sizes attribute to enhance responsive image functionality 590 * for post thumbnails 591 * 592 * @since Twenty Sixteen 1.0 593 * 594 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. 595 * See wp_get_attachment_image(). 596 * @param WP_Post $attachment Image attachment post. 597 * @param string|int[] $size Requested image size. Can be any registered image size name, or 598 * an array of width and height values in pixels (in that order). 599 * @return string[] The filtered attributes for the image markup. 600 */ 601 function twentysixteen_post_thumbnail_sizes_attr( $attr, $attachment, $size ) { 602 if ( 'post-thumbnail' === $size ) { 603 if ( is_active_sidebar( 'sidebar-1' ) ) { 604 $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 60vw, (max-width: 1362px) 62vw, 840px'; 605 } else { 606 $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 88vw, 1200px'; 607 } 608 } 609 return $attr; 610 } 611 add_filter( 'wp_get_attachment_image_attributes', 'twentysixteen_post_thumbnail_sizes_attr', 10, 3 ); 612 613 /** 614 * Modifies tag cloud widget arguments to display all tags in the same font size 615 * and use list format for better accessibility. 616 * 617 * @since Twenty Sixteen 1.1 618 * 619 * @param array $args Arguments for tag cloud widget. 620 * @return array The filtered arguments for tag cloud widget. 621 */ 622 function twentysixteen_widget_tag_cloud_args( $args ) { 623 $args['largest'] = 1; 624 $args['smallest'] = 1; 625 $args['unit'] = 'em'; 626 $args['format'] = 'list'; 627 628 return $args; 629 } 630 add_filter( 'widget_tag_cloud_args', 'twentysixteen_widget_tag_cloud_args' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Jul 1 08:20:12 2026 | Cross-referenced by PHPXref |