[ 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 * 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. 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 * Add 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 * Register 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 echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n"; 377 } 378 add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 ); 379 380 /** 381 * Enqueues scripts and styles. 382 * 383 * @since Twenty Sixteen 1.0 384 */ 385 function twentysixteen_scripts() { 386 // Add custom fonts, used in the main stylesheet. 387 $font_version = ( 0 === strpos( (string) twentysixteen_fonts_url(), get_template_directory_uri() . '/' ) ) ? '20230328' : null; 388 wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), $font_version ); 389 390 // Add Genericons, used in the main stylesheet. 391 wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '20201208' ); 392 393 // Theme stylesheet. 394 wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri(), array(), '20241112' ); 395 396 // Theme block stylesheet. 397 wp_enqueue_style( 'twentysixteen-block-style', get_template_directory_uri() . '/css/blocks.css', array( 'twentysixteen-style' ), '20240817' ); 398 399 // Load the Internet Explorer specific stylesheet. 400 wp_enqueue_style( 'twentysixteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentysixteen-style' ), '20170530' ); 401 wp_style_add_data( 'twentysixteen-ie', 'conditional', 'lt IE 10' ); 402 403 // Load the Internet Explorer 8 specific stylesheet. 404 wp_enqueue_style( 'twentysixteen-ie8', get_template_directory_uri() . '/css/ie8.css', array( 'twentysixteen-style' ), '20170530' ); 405 wp_style_add_data( 'twentysixteen-ie8', 'conditional', 'lt IE 9' ); 406 407 // Load the Internet Explorer 7 specific stylesheet. 408 wp_enqueue_style( 'twentysixteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentysixteen-style' ), '20170530' ); 409 wp_style_add_data( 'twentysixteen-ie7', 'conditional', 'lt IE 8' ); 410 411 // Load the html5 shiv. 412 wp_enqueue_script( 'twentysixteen-html5', get_template_directory_uri() . '/js/html5.js', array(), '3.7.3' ); 413 wp_script_add_data( 'twentysixteen-html5', 'conditional', 'lt IE 9' ); 414 415 // Skip-link fix is no longer enqueued by default. 416 wp_register_script( 'twentysixteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20230526', array( 'in_footer' => true ) ); 417 418 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { 419 wp_enqueue_script( 'comment-reply' ); 420 } 421 422 if ( is_singular() && wp_attachment_is_image() ) { 423 wp_enqueue_script( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20170530' ); 424 } 425 426 wp_enqueue_script( 427 'twentysixteen-script', 428 get_template_directory_uri() . '/js/functions.js', 429 array( 'jquery' ), 430 '20230629', 431 array( 432 'in_footer' => false, // Because involves header. 433 'strategy' => 'defer', 434 ) 435 ); 436 437 wp_localize_script( 438 'twentysixteen-script', 439 'screenReaderText', 440 array( 441 'expand' => __( 'expand child menu', 'twentysixteen' ), 442 'collapse' => __( 'collapse child menu', 'twentysixteen' ), 443 ) 444 ); 445 } 446 add_action( 'wp_enqueue_scripts', 'twentysixteen_scripts' ); 447 448 /** 449 * Enqueue styles for the block-based editor. 450 * 451 * @since Twenty Sixteen 1.6 452 */ 453 function twentysixteen_block_editor_styles() { 454 // Block styles. 455 wp_enqueue_style( 'twentysixteen-block-editor-style', get_template_directory_uri() . '/css/editor-blocks.css', array(), '20240817' ); 456 // Add custom fonts. 457 $font_version = ( 0 === strpos( (string) twentysixteen_fonts_url(), get_template_directory_uri() . '/' ) ) ? '20230328' : null; 458 wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), $font_version ); 459 } 460 add_action( 'enqueue_block_editor_assets', 'twentysixteen_block_editor_styles' ); 461 462 /** 463 * Adds custom classes to the array of body classes. 464 * 465 * @since Twenty Sixteen 1.0 466 * 467 * @param array $classes Classes for the body element. 468 * @return array (Maybe) filtered body classes. 469 */ 470 function twentysixteen_body_classes( $classes ) { 471 // Adds a class of custom-background-image to sites with a custom background image. 472 if ( get_background_image() ) { 473 $classes[] = 'custom-background-image'; 474 } 475 476 // Adds a class of group-blog to sites with more than 1 published author. 477 if ( is_multi_author() ) { 478 $classes[] = 'group-blog'; 479 } 480 481 // Adds a class of no-sidebar to sites without active sidebar. 482 if ( ! is_active_sidebar( 'sidebar-1' ) ) { 483 $classes[] = 'no-sidebar'; 484 } 485 486 // Adds a class of hfeed to non-singular pages. 487 if ( ! is_singular() ) { 488 $classes[] = 'hfeed'; 489 } 490 491 return $classes; 492 } 493 add_filter( 'body_class', 'twentysixteen_body_classes' ); 494 495 /** 496 * Converts a HEX value to RGB. 497 * 498 * @since Twenty Sixteen 1.0 499 * 500 * @param string $color The original color, in 3- or 6-digit hexadecimal form. 501 * @return array Array containing RGB (red, green, and blue) values for the given 502 * HEX code, empty array otherwise. 503 */ 504 function twentysixteen_hex2rgb( $color ) { 505 $color = trim( $color, '#' ); 506 507 if ( strlen( $color ) === 3 ) { 508 $r = hexdec( substr( $color, 0, 1 ) . substr( $color, 0, 1 ) ); 509 $g = hexdec( substr( $color, 1, 1 ) . substr( $color, 1, 1 ) ); 510 $b = hexdec( substr( $color, 2, 1 ) . substr( $color, 2, 1 ) ); 511 } elseif ( strlen( $color ) === 6 ) { 512 $r = hexdec( substr( $color, 0, 2 ) ); 513 $g = hexdec( substr( $color, 2, 2 ) ); 514 $b = hexdec( substr( $color, 4, 2 ) ); 515 } else { 516 return array(); 517 } 518 519 return array( 520 'red' => $r, 521 'green' => $g, 522 'blue' => $b, 523 ); 524 } 525 526 /** 527 * Custom template tags for this theme. 528 */ 529 require get_template_directory() . '/inc/template-tags.php'; 530 531 532 /** 533 * Register block patterns and pattern categories. 534 * 535 * @since Twenty Sixteen 3.4 536 */ 537 function twentysixteen_register_block_patterns() { 538 require get_template_directory() . '/inc/block-patterns.php'; 539 } 540 541 add_action( 'init', 'twentysixteen_register_block_patterns' ); 542 543 /** 544 * Customizer additions. 545 */ 546 require get_template_directory() . '/inc/customizer.php'; 547 548 /** 549 * Add custom image sizes attribute to enhance responsive image functionality 550 * for content images 551 * 552 * @since Twenty Sixteen 1.0 553 * 554 * @param string $sizes A source size value for use in a 'sizes' attribute. 555 * @param array $size Image size. Accepts an array of width and height 556 * values in pixels (in that order). 557 * @return string A source size value for use in a content image 'sizes' attribute. 558 */ 559 function twentysixteen_content_image_sizes_attr( $sizes, $size ) { 560 $width = $size[0]; 561 562 if ( 840 <= $width ) { 563 $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px'; 564 } 565 566 if ( 'page' === get_post_type() ) { 567 if ( 840 > $width ) { 568 $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px'; 569 } 570 } else { 571 if ( 840 > $width && 600 <= $width ) { 572 $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px'; 573 } elseif ( 600 > $width ) { 574 $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px'; 575 } 576 } 577 578 return $sizes; 579 } 580 add_filter( 'wp_calculate_image_sizes', 'twentysixteen_content_image_sizes_attr', 10, 2 ); 581 582 /** 583 * Add custom image sizes attribute to enhance responsive image functionality 584 * for post thumbnails 585 * 586 * @since Twenty Sixteen 1.0 587 * 588 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. 589 * See wp_get_attachment_image(). 590 * @param WP_Post $attachment Image attachment post. 591 * @param string|int[] $size Requested image size. Can be any registered image size name, or 592 * an array of width and height values in pixels (in that order). 593 * @return string[] The filtered attributes for the image markup. 594 */ 595 function twentysixteen_post_thumbnail_sizes_attr( $attr, $attachment, $size ) { 596 if ( 'post-thumbnail' === $size ) { 597 if ( is_active_sidebar( 'sidebar-1' ) ) { 598 $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 60vw, (max-width: 1362px) 62vw, 840px'; 599 } else { 600 $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 88vw, 1200px'; 601 } 602 } 603 return $attr; 604 } 605 add_filter( 'wp_get_attachment_image_attributes', 'twentysixteen_post_thumbnail_sizes_attr', 10, 3 ); 606 607 /** 608 * Modifies tag cloud widget arguments to display all tags in the same font size 609 * and use list format for better accessibility. 610 * 611 * @since Twenty Sixteen 1.1 612 * 613 * @param array $args Arguments for tag cloud widget. 614 * @return array The filtered arguments for tag cloud widget. 615 */ 616 function twentysixteen_widget_tag_cloud_args( $args ) { 617 $args['largest'] = 1; 618 $args['smallest'] = 1; 619 $args['unit'] = 'em'; 620 $args['format'] = 'list'; 621 622 return $args; 623 } 624 add_filter( 'widget_tag_cloud_args', 'twentysixteen_widget_tag_cloud_args' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |