[ 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 $GLOBALS['content_width'] = apply_filters( 'twentysixteen_content_width', 840 ); 250 } 251 add_action( 'after_setup_theme', 'twentysixteen_content_width', 0 ); 252 253 /** 254 * Adds preconnect for Google Fonts. 255 * 256 * @since Twenty Sixteen 1.6 257 * @deprecated Twenty Sixteen 2.9 Disabled filter because, by default, fonts are self-hosted. 258 * 259 * @param array $urls URLs to print for resource hints. 260 * @param string $relation_type The relation type the URLs are printed. 261 * @return array URLs to print for resource hints. 262 */ 263 function twentysixteen_resource_hints( $urls, $relation_type ) { 264 if ( wp_style_is( 'twentysixteen-fonts', 'queue' ) && 'preconnect' === $relation_type ) { 265 $urls[] = array( 266 'href' => 'https://fonts.gstatic.com', 267 'crossorigin', 268 ); 269 } 270 271 return $urls; 272 } 273 // add_filter( 'wp_resource_hints', 'twentysixteen_resource_hints', 10, 2 ); 274 275 /** 276 * Registers a widget area. 277 * 278 * @link https://developer.wordpress.org/reference/functions/register_sidebar/ 279 * 280 * @since Twenty Sixteen 1.0 281 */ 282 function twentysixteen_widgets_init() { 283 register_sidebar( 284 array( 285 'name' => __( 'Sidebar', 'twentysixteen' ), 286 'id' => 'sidebar-1', 287 'description' => __( 'Add widgets here to appear in your sidebar.', 'twentysixteen' ), 288 'before_widget' => '<section id="%1$s" class="widget %2$s">', 289 'after_widget' => '</section>', 290 'before_title' => '<h2 class="widget-title">', 291 'after_title' => '</h2>', 292 ) 293 ); 294 295 register_sidebar( 296 array( 297 'name' => __( 'Content Bottom 1', 'twentysixteen' ), 298 'id' => 'sidebar-2', 299 'description' => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ), 300 'before_widget' => '<section id="%1$s" class="widget %2$s">', 301 'after_widget' => '</section>', 302 'before_title' => '<h2 class="widget-title">', 303 'after_title' => '</h2>', 304 ) 305 ); 306 307 register_sidebar( 308 array( 309 'name' => __( 'Content Bottom 2', 'twentysixteen' ), 310 'id' => 'sidebar-3', 311 'description' => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ), 312 'before_widget' => '<section id="%1$s" class="widget %2$s">', 313 'after_widget' => '</section>', 314 'before_title' => '<h2 class="widget-title">', 315 'after_title' => '</h2>', 316 ) 317 ); 318 } 319 add_action( 'widgets_init', 'twentysixteen_widgets_init' ); 320 321 if ( ! function_exists( 'twentysixteen_fonts_url' ) ) : 322 /** 323 * Registers fonts for Twenty Sixteen. 324 * 325 * Create your own twentysixteen_fonts_url() function to override in a child theme. 326 * 327 * @since Twenty Sixteen 1.0 328 * @since Twenty Sixteen 2.9 Replaced Google URL with self-hosted fonts. 329 * 330 * @return string Fonts URL for the theme. 331 */ 332 function twentysixteen_fonts_url() { 333 $fonts_url = ''; 334 $fonts = array(); 335 336 /* 337 * translators: If there are characters in your language that are not supported 338 * by Merriweather, translate this to 'off'. Do not translate into your own language. 339 */ 340 if ( 'off' !== _x( 'on', 'Merriweather font: on or off', 'twentysixteen' ) ) { 341 $fonts[] = 'merriweather'; 342 } 343 344 /* 345 * translators: If there are characters in your language that are not supported 346 * by Montserrat, translate this to 'off'. Do not translate into your own language. 347 */ 348 if ( 'off' !== _x( 'on', 'Montserrat font: on or off', 'twentysixteen' ) ) { 349 $fonts[] = 'montserrat'; 350 } 351 352 /* 353 * translators: If there are characters in your language that are not supported 354 * by Inconsolata, translate this to 'off'. Do not translate into your own language. 355 */ 356 if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentysixteen' ) ) { 357 $fonts[] = 'inconsolata'; 358 } 359 360 if ( $fonts ) { 361 $fonts_url = get_template_directory_uri() . '/fonts/' . implode( '-plus-', $fonts ) . '.css'; 362 } 363 364 return $fonts_url; 365 } 366 endif; 367 368 /** 369 * Handles JavaScript detection. 370 * 371 * Adds a `js` class to the root `<html>` element when JavaScript is detected. 372 * 373 * @since Twenty Sixteen 1.0 374 */ 375 function twentysixteen_javascript_detection() { 376 $js = "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);"; 377 $js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ ); 378 379 if ( function_exists( 'wp_print_inline_script_tag' ) ) { 380 wp_print_inline_script_tag( $js ); 381 } else { 382 echo "<script>$js</script>\n"; 383 } 384 } 385 add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 ); 386 387 /** 388 * Enqueues scripts and styles. 389 * 390 * @since Twenty Sixteen 1.0 391 */ 392 function twentysixteen_scripts() { 393 // Add custom fonts, used in the main stylesheet. 394 $font_version = ( 0 === strpos( (string) twentysixteen_fonts_url(), get_template_directory_uri() . '/' ) ) ? '20230328' : null; 395 wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), $font_version ); 396 397 // Add Genericons, used in the main stylesheet. 398 wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '20201208' ); 399 400 // Theme stylesheet. 401 wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri(), array(), '20250715' ); 402 403 // Theme block stylesheet. 404 wp_enqueue_style( 'twentysixteen-block-style', get_template_directory_uri() . '/css/blocks.css', array( 'twentysixteen-style' ), '20240817' ); 405 406 // Load the Internet Explorer specific stylesheet. 407 wp_enqueue_style( 'twentysixteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentysixteen-style' ), '20170530' ); 408 wp_style_add_data( 'twentysixteen-ie', 'conditional', 'lt IE 10' ); 409 410 // Load the Internet Explorer 8 specific stylesheet. 411 wp_enqueue_style( 'twentysixteen-ie8', get_template_directory_uri() . '/css/ie8.css', array( 'twentysixteen-style' ), '20170530' ); 412 wp_style_add_data( 'twentysixteen-ie8', 'conditional', 'lt IE 9' ); 413 414 // Load the Internet Explorer 7 specific stylesheet. 415 wp_enqueue_style( 'twentysixteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentysixteen-style' ), '20170530' ); 416 wp_style_add_data( 'twentysixteen-ie7', 'conditional', 'lt IE 8' ); 417 418 // Load the html5 shiv. 419 wp_enqueue_script( 'twentysixteen-html5', get_template_directory_uri() . '/js/html5.js', array(), '3.7.3' ); 420 wp_script_add_data( 'twentysixteen-html5', 'conditional', 'lt IE 9' ); 421 422 // Skip-link fix is no longer enqueued by default. 423 wp_register_script( 'twentysixteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20230526', array( 'in_footer' => true ) ); 424 425 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { 426 wp_enqueue_script( 'comment-reply' ); 427 } 428 429 if ( is_singular() && wp_attachment_is_image() ) { 430 wp_enqueue_script( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20170530' ); 431 } 432 433 wp_enqueue_script( 434 'twentysixteen-script', 435 get_template_directory_uri() . '/js/functions.js', 436 array( 'jquery' ), 437 '20230629', 438 array( 439 'in_footer' => false, // Because involves header. 440 'strategy' => 'defer', 441 ) 442 ); 443 444 wp_localize_script( 445 'twentysixteen-script', 446 'screenReaderText', 447 array( 448 'expand' => __( 'expand child menu', 'twentysixteen' ), 449 'collapse' => __( 'collapse child menu', 'twentysixteen' ), 450 ) 451 ); 452 } 453 add_action( 'wp_enqueue_scripts', 'twentysixteen_scripts' ); 454 455 /** 456 * Enqueues styles for the block-based editor. 457 * 458 * @since Twenty Sixteen 1.6 459 */ 460 function twentysixteen_block_editor_styles() { 461 // Block styles. 462 wp_enqueue_style( 'twentysixteen-block-editor-style', get_template_directory_uri() . '/css/editor-blocks.css', array(), '20250715' ); 463 // Add custom fonts. 464 $font_version = ( 0 === strpos( (string) twentysixteen_fonts_url(), get_template_directory_uri() . '/' ) ) ? '20230328' : null; 465 wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), $font_version ); 466 } 467 add_action( 'enqueue_block_editor_assets', 'twentysixteen_block_editor_styles' ); 468 469 /** 470 * Adds custom classes to the array of body classes. 471 * 472 * @since Twenty Sixteen 1.0 473 * 474 * @param array $classes Classes for the body element. 475 * @return array (Maybe) filtered body classes. 476 */ 477 function twentysixteen_body_classes( $classes ) { 478 // Adds a class of custom-background-image to sites with a custom background image. 479 if ( get_background_image() ) { 480 $classes[] = 'custom-background-image'; 481 } 482 483 // Adds a class of group-blog to sites with more than 1 published author. 484 if ( is_multi_author() ) { 485 $classes[] = 'group-blog'; 486 } 487 488 // Adds a class of no-sidebar to sites without active sidebar. 489 if ( ! is_active_sidebar( 'sidebar-1' ) ) { 490 $classes[] = 'no-sidebar'; 491 } 492 493 // Adds a class of hfeed to non-singular pages. 494 if ( ! is_singular() ) { 495 $classes[] = 'hfeed'; 496 } 497 498 return $classes; 499 } 500 add_filter( 'body_class', 'twentysixteen_body_classes' ); 501 502 /** 503 * Converts a HEX value to RGB. 504 * 505 * @since Twenty Sixteen 1.0 506 * 507 * @param string $color The original color, in 3- or 6-digit hexadecimal form. 508 * @return array Array containing RGB (red, green, and blue) values for the given 509 * HEX code, empty array otherwise. 510 */ 511 function twentysixteen_hex2rgb( $color ) { 512 $color = trim( $color, '#' ); 513 514 if ( strlen( $color ) === 3 ) { 515 $r = hexdec( substr( $color, 0, 1 ) . substr( $color, 0, 1 ) ); 516 $g = hexdec( substr( $color, 1, 1 ) . substr( $color, 1, 1 ) ); 517 $b = hexdec( substr( $color, 2, 1 ) . substr( $color, 2, 1 ) ); 518 } elseif ( strlen( $color ) === 6 ) { 519 $r = hexdec( substr( $color, 0, 2 ) ); 520 $g = hexdec( substr( $color, 2, 2 ) ); 521 $b = hexdec( substr( $color, 4, 2 ) ); 522 } else { 523 return array(); 524 } 525 526 return array( 527 'red' => $r, 528 'green' => $g, 529 'blue' => $b, 530 ); 531 } 532 533 /** 534 * Custom template tags for this theme. 535 */ 536 require get_template_directory() . '/inc/template-tags.php'; 537 538 539 /** 540 * Registers block patterns and pattern categories. 541 * 542 * @since Twenty Sixteen 3.4 543 */ 544 function twentysixteen_register_block_patterns() { 545 require get_template_directory() . '/inc/block-patterns.php'; 546 } 547 548 add_action( 'init', 'twentysixteen_register_block_patterns' ); 549 550 /** 551 * Customizer additions. 552 */ 553 require get_template_directory() . '/inc/customizer.php'; 554 555 /** 556 * Adds custom image sizes attribute to enhance responsive image functionality 557 * for content images 558 * 559 * @since Twenty Sixteen 1.0 560 * 561 * @param string $sizes A source size value for use in a 'sizes' attribute. 562 * @param array $size Image size. Accepts an array of width and height 563 * values in pixels (in that order). 564 * @return string A source size value for use in a content image 'sizes' attribute. 565 */ 566 function twentysixteen_content_image_sizes_attr( $sizes, $size ) { 567 $width = $size[0]; 568 569 if ( 840 <= $width ) { 570 $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px'; 571 } 572 573 if ( 'page' === get_post_type() ) { 574 if ( 840 > $width ) { 575 $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px'; 576 } 577 } else { 578 if ( 840 > $width && 600 <= $width ) { 579 $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px'; 580 } elseif ( 600 > $width ) { 581 $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px'; 582 } 583 } 584 585 return $sizes; 586 } 587 add_filter( 'wp_calculate_image_sizes', 'twentysixteen_content_image_sizes_attr', 10, 2 ); 588 589 /** 590 * Adds custom image sizes attribute to enhance responsive image functionality 591 * for post thumbnails 592 * 593 * @since Twenty Sixteen 1.0 594 * 595 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. 596 * See wp_get_attachment_image(). 597 * @param WP_Post $attachment Image attachment post. 598 * @param string|int[] $size Requested image size. Can be any registered image size name, or 599 * an array of width and height values in pixels (in that order). 600 * @return string[] The filtered attributes for the image markup. 601 */ 602 function twentysixteen_post_thumbnail_sizes_attr( $attr, $attachment, $size ) { 603 if ( 'post-thumbnail' === $size ) { 604 if ( is_active_sidebar( 'sidebar-1' ) ) { 605 $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 60vw, (max-width: 1362px) 62vw, 840px'; 606 } else { 607 $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 88vw, 1200px'; 608 } 609 } 610 return $attr; 611 } 612 add_filter( 'wp_get_attachment_image_attributes', 'twentysixteen_post_thumbnail_sizes_attr', 10, 3 ); 613 614 /** 615 * Modifies tag cloud widget arguments to display all tags in the same font size 616 * and use list format for better accessibility. 617 * 618 * @since Twenty Sixteen 1.1 619 * 620 * @param array $args Arguments for tag cloud widget. 621 * @return array The filtered arguments for tag cloud widget. 622 */ 623 function twentysixteen_widget_tag_cloud_args( $args ) { 624 $args['largest'] = 1; 625 $args['smallest'] = 1; 626 $args['unit'] = 'em'; 627 $args['format'] = 'list'; 628 629 return $args; 630 } 631 add_filter( 'widget_tag_cloud_args', 'twentysixteen_widget_tag_cloud_args' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Fri Oct 10 08:20:03 2025 | Cross-referenced by PHPXref |