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