| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Functions and definitions 4 * 5 * @link https://developer.wordpress.org/themes/basics/theme-functions/ 6 * 7 * @package WordPress 8 * @subpackage Twenty_Twenty_One 9 * @since Twenty Twenty-One 1.0 10 */ 11 12 // This theme requires WordPress 5.3 or later. 13 if ( version_compare( $GLOBALS['wp_version'], '5.3', '<' ) ) { 14 require get_template_directory() . '/inc/back-compat.php'; 15 } 16 17 if ( ! function_exists( 'twenty_twenty_one_setup' ) ) { 18 /** 19 * Sets up theme defaults and registers support for various WordPress features. 20 * 21 * Note that this function is hooked into the after_setup_theme hook, which 22 * runs before the init hook. The init hook is too late for some features, such 23 * as indicating support for post thumbnails. 24 * 25 * @since Twenty Twenty-One 1.0 26 * 27 * @return void 28 */ 29 function twenty_twenty_one_setup() { 30 31 // Add default posts and comments RSS feed links to head. 32 add_theme_support( 'automatic-feed-links' ); 33 34 /* 35 * Let WordPress manage the document title. 36 * This theme does not use a hard-coded <title> tag in the document head, 37 * WordPress will provide it for us. 38 */ 39 add_theme_support( 'title-tag' ); 40 41 /** 42 * Add post-formats support. 43 */ 44 add_theme_support( 45 'post-formats', 46 array( 47 'link', 48 'aside', 49 'gallery', 50 'image', 51 'quote', 52 'status', 53 'video', 54 'audio', 55 'chat', 56 ) 57 ); 58 59 /* 60 * Enable support for Post Thumbnails on posts and pages. 61 * 62 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/ 63 */ 64 add_theme_support( 'post-thumbnails' ); 65 set_post_thumbnail_size( 1568, 9999 ); 66 67 register_nav_menus( 68 array( 69 'primary' => esc_html__( 'Primary menu', 'twentytwentyone' ), 70 'footer' => esc_html__( 'Secondary menu', 'twentytwentyone' ), 71 ) 72 ); 73 74 /* 75 * Switch default core markup for search form, comment form, and comments 76 * to output valid HTML5. 77 */ 78 add_theme_support( 79 'html5', 80 array( 81 'comment-form', 82 'comment-list', 83 'gallery', 84 'caption', 85 'style', 86 'script', 87 'navigation-widgets', 88 ) 89 ); 90 91 /* 92 * Add support for core custom logo. 93 * 94 * @link https://codex.wordpress.org/Theme_Logo 95 */ 96 $logo_width = 300; 97 $logo_height = 100; 98 99 add_theme_support( 100 'custom-logo', 101 array( 102 'height' => $logo_height, 103 'width' => $logo_width, 104 'flex-width' => true, 105 'flex-height' => true, 106 'unlink-homepage-logo' => true, 107 ) 108 ); 109 110 // Add theme support for selective refresh for widgets. 111 add_theme_support( 'customize-selective-refresh-widgets' ); 112 113 // Add support for Block Styles. 114 add_theme_support( 'wp-block-styles' ); 115 116 // Add support for full and wide align images. 117 add_theme_support( 'align-wide' ); 118 119 // Add support for editor styles. 120 add_theme_support( 'editor-styles' ); 121 $background_color = get_theme_mod( 'background_color', 'D1E4DD' ); 122 if ( 127 > Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( $background_color ) ) { 123 add_theme_support( 'dark-editor-style' ); 124 } 125 126 $editor_stylesheet_path = './assets/css/style-editor.css'; 127 128 // Note, the is_IE global variable is defined by WordPress and is used 129 // to detect if the current browser is internet explorer. 130 global $is_IE; 131 if ( $is_IE ) { 132 $editor_stylesheet_path = './assets/css/ie-editor.css'; 133 } 134 135 // Enqueue editor styles. 136 add_editor_style( $editor_stylesheet_path ); 137 138 // Add custom editor font sizes. 139 add_theme_support( 140 'editor-font-sizes', 141 array( 142 array( 143 'name' => esc_html__( 'Extra small', 'twentytwentyone' ), 144 'shortName' => esc_html_x( 'XS', 'Font size', 'twentytwentyone' ), 145 'size' => 16, 146 'slug' => 'extra-small', 147 ), 148 array( 149 'name' => esc_html__( 'Small', 'twentytwentyone' ), 150 'shortName' => esc_html_x( 'S', 'Font size', 'twentytwentyone' ), 151 'size' => 18, 152 'slug' => 'small', 153 ), 154 array( 155 'name' => esc_html__( 'Normal', 'twentytwentyone' ), 156 'shortName' => esc_html_x( 'M', 'Font size', 'twentytwentyone' ), 157 'size' => 20, 158 'slug' => 'normal', 159 ), 160 array( 161 'name' => esc_html__( 'Large', 'twentytwentyone' ), 162 'shortName' => esc_html_x( 'L', 'Font size', 'twentytwentyone' ), 163 'size' => 24, 164 'slug' => 'large', 165 ), 166 array( 167 'name' => esc_html__( 'Extra large', 'twentytwentyone' ), 168 'shortName' => esc_html_x( 'XL', 'Font size', 'twentytwentyone' ), 169 'size' => 40, 170 'slug' => 'extra-large', 171 ), 172 array( 173 'name' => esc_html__( 'Huge', 'twentytwentyone' ), 174 'shortName' => esc_html_x( 'XXL', 'Font size', 'twentytwentyone' ), 175 'size' => 96, 176 'slug' => 'huge', 177 ), 178 array( 179 'name' => esc_html__( 'Gigantic', 'twentytwentyone' ), 180 'shortName' => esc_html_x( 'XXXL', 'Font size', 'twentytwentyone' ), 181 'size' => 144, 182 'slug' => 'gigantic', 183 ), 184 ) 185 ); 186 187 // Custom background color. 188 add_theme_support( 189 'custom-background', 190 array( 191 'default-color' => 'd1e4dd', 192 ) 193 ); 194 195 // Editor color palette. 196 $black = '#000000'; 197 $dark_gray = '#28303D'; 198 $gray = '#39414D'; 199 $green = '#D1E4DD'; 200 $blue = '#D1DFE4'; 201 $purple = '#D1D1E4'; 202 $red = '#E4D1D1'; 203 $orange = '#E4DAD1'; 204 $yellow = '#EEEADD'; 205 $white = '#FFFFFF'; 206 207 add_theme_support( 208 'editor-color-palette', 209 array( 210 array( 211 'name' => esc_html__( 'Black', 'twentytwentyone' ), 212 'slug' => 'black', 213 'color' => $black, 214 ), 215 array( 216 'name' => esc_html__( 'Dark gray', 'twentytwentyone' ), 217 'slug' => 'dark-gray', 218 'color' => $dark_gray, 219 ), 220 array( 221 'name' => esc_html__( 'Gray', 'twentytwentyone' ), 222 'slug' => 'gray', 223 'color' => $gray, 224 ), 225 array( 226 'name' => esc_html__( 'Green', 'twentytwentyone' ), 227 'slug' => 'green', 228 'color' => $green, 229 ), 230 array( 231 'name' => esc_html__( 'Blue', 'twentytwentyone' ), 232 'slug' => 'blue', 233 'color' => $blue, 234 ), 235 array( 236 'name' => esc_html__( 'Purple', 'twentytwentyone' ), 237 'slug' => 'purple', 238 'color' => $purple, 239 ), 240 array( 241 'name' => esc_html__( 'Red', 'twentytwentyone' ), 242 'slug' => 'red', 243 'color' => $red, 244 ), 245 array( 246 'name' => esc_html__( 'Orange', 'twentytwentyone' ), 247 'slug' => 'orange', 248 'color' => $orange, 249 ), 250 array( 251 'name' => esc_html__( 'Yellow', 'twentytwentyone' ), 252 'slug' => 'yellow', 253 'color' => $yellow, 254 ), 255 array( 256 'name' => esc_html__( 'White', 'twentytwentyone' ), 257 'slug' => 'white', 258 'color' => $white, 259 ), 260 ) 261 ); 262 263 add_theme_support( 264 'editor-gradient-presets', 265 array( 266 array( 267 'name' => esc_html__( 'Purple to yellow', 'twentytwentyone' ), 268 'gradient' => 'linear-gradient(160deg, ' . $purple . ' 0%, ' . $yellow . ' 100%)', 269 'slug' => 'purple-to-yellow', 270 ), 271 array( 272 'name' => esc_html__( 'Yellow to purple', 'twentytwentyone' ), 273 'gradient' => 'linear-gradient(160deg, ' . $yellow . ' 0%, ' . $purple . ' 100%)', 274 'slug' => 'yellow-to-purple', 275 ), 276 array( 277 'name' => esc_html__( 'Green to yellow', 'twentytwentyone' ), 278 'gradient' => 'linear-gradient(160deg, ' . $green . ' 0%, ' . $yellow . ' 100%)', 279 'slug' => 'green-to-yellow', 280 ), 281 array( 282 'name' => esc_html__( 'Yellow to green', 'twentytwentyone' ), 283 'gradient' => 'linear-gradient(160deg, ' . $yellow . ' 0%, ' . $green . ' 100%)', 284 'slug' => 'yellow-to-green', 285 ), 286 array( 287 'name' => esc_html__( 'Red to yellow', 'twentytwentyone' ), 288 'gradient' => 'linear-gradient(160deg, ' . $red . ' 0%, ' . $yellow . ' 100%)', 289 'slug' => 'red-to-yellow', 290 ), 291 array( 292 'name' => esc_html__( 'Yellow to red', 'twentytwentyone' ), 293 'gradient' => 'linear-gradient(160deg, ' . $yellow . ' 0%, ' . $red . ' 100%)', 294 'slug' => 'yellow-to-red', 295 ), 296 array( 297 'name' => esc_html__( 'Purple to red', 'twentytwentyone' ), 298 'gradient' => 'linear-gradient(160deg, ' . $purple . ' 0%, ' . $red . ' 100%)', 299 'slug' => 'purple-to-red', 300 ), 301 array( 302 'name' => esc_html__( 'Red to purple', 'twentytwentyone' ), 303 'gradient' => 'linear-gradient(160deg, ' . $red . ' 0%, ' . $purple . ' 100%)', 304 'slug' => 'red-to-purple', 305 ), 306 ) 307 ); 308 309 /* 310 * Adds starter content to highlight the theme on fresh sites. 311 * This is done conditionally to avoid loading the starter content on every 312 * page load, as it is a one-off operation only needed once in the customizer. 313 */ 314 if ( is_customize_preview() ) { 315 require get_template_directory() . '/inc/starter-content.php'; 316 add_theme_support( 'starter-content', twenty_twenty_one_get_starter_content() ); 317 } 318 319 // Add support for responsive embedded content. 320 add_theme_support( 'responsive-embeds' ); 321 322 // Add support for custom line height controls. 323 add_theme_support( 'custom-line-height' ); 324 325 // Add support for link color control. 326 add_theme_support( 'link-color' ); 327 328 // Add support for experimental cover block spacing. 329 add_theme_support( 'custom-spacing' ); 330 331 // Add support for custom units. 332 // This was removed in WordPress 5.6 but is still required to properly support WP 5.5. 333 add_theme_support( 'custom-units' ); 334 335 // Remove feed icon link from legacy RSS widget. 336 add_filter( 'rss_widget_feed_link', '__return_empty_string' ); 337 } 338 } 339 add_action( 'after_setup_theme', 'twenty_twenty_one_setup' ); 340 341 /** 342 * Registers widget area. 343 * 344 * @since Twenty Twenty-One 1.0 345 * 346 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar 347 * 348 * @return void 349 */ 350 function twenty_twenty_one_widgets_init() { 351 352 register_sidebar( 353 array( 354 'name' => esc_html__( 'Footer', 'twentytwentyone' ), 355 'id' => 'sidebar-1', 356 'description' => esc_html__( 'Add widgets here to appear in your footer.', 'twentytwentyone' ), 357 'before_widget' => '<section id="%1$s" class="widget %2$s">', 358 'after_widget' => '</section>', 359 'before_title' => '<h2 class="widget-title">', 360 'after_title' => '</h2>', 361 ) 362 ); 363 } 364 add_action( 'widgets_init', 'twenty_twenty_one_widgets_init' ); 365 366 /** 367 * Sets the content width in pixels, based on the theme's design and stylesheet. 368 * 369 * Priority 0 to make it available to lower priority callbacks. 370 * 371 * @since Twenty Twenty-One 1.0 372 * 373 * @global int $content_width Content width. 374 * 375 * @return void 376 */ 377 function twenty_twenty_one_content_width() { 378 /** 379 * Filters Twenty Twenty-One content width of the theme. 380 * 381 * @since Twenty Twenty-One 1.0 382 * 383 * @param int $content_width Content width in pixels. 384 */ 385 $GLOBALS['content_width'] = apply_filters( 'twenty_twenty_one_content_width', 750 ); 386 } 387 add_action( 'after_setup_theme', 'twenty_twenty_one_content_width', 0 ); 388 389 /** 390 * Enqueues scripts and styles. 391 * 392 * @since Twenty Twenty-One 1.0 393 * 394 * @global bool $is_IE 395 * @global WP_Scripts $wp_scripts 396 * 397 * @return void 398 */ 399 function twenty_twenty_one_scripts() { 400 // Note, the is_IE global variable is defined by WordPress and is used 401 // to detect if the current browser is internet explorer. 402 global $is_IE, $wp_scripts; 403 if ( $is_IE ) { 404 // If IE 11 or below, use a flattened stylesheet with static values replacing CSS Variables. 405 wp_enqueue_style( 'twenty-twenty-one-style', get_template_directory_uri() . '/assets/css/ie.css', array(), wp_get_theme()->get( 'Version' ) ); 406 } else { 407 // If not IE, use the standard stylesheet. 408 wp_enqueue_style( 'twenty-twenty-one-style', get_template_directory_uri() . '/style.css', array(), wp_get_theme()->get( 'Version' ) ); 409 } 410 411 // RTL styles. 412 wp_style_add_data( 'twenty-twenty-one-style', 'rtl', 'replace' ); 413 414 // Print styles. 415 wp_enqueue_style( 'twenty-twenty-one-print-style', get_template_directory_uri() . '/assets/css/print.css', array(), wp_get_theme()->get( 'Version' ), 'print' ); 416 417 // Threaded comment reply styles. 418 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { 419 wp_enqueue_script( 'comment-reply' ); 420 } 421 422 // Register the IE11 polyfill file. 423 wp_register_script( 424 'twenty-twenty-one-ie11-polyfills-asset', 425 get_template_directory_uri() . '/assets/js/polyfills.js', 426 array(), 427 wp_get_theme()->get( 'Version' ), 428 array( 'in_footer' => true ) 429 ); 430 431 // Register the IE11 polyfill loader. 432 wp_register_script( 433 'twenty-twenty-one-ie11-polyfills', 434 null, 435 array(), 436 wp_get_theme()->get( 'Version' ), 437 array( 'in_footer' => true ) 438 ); 439 wp_add_inline_script( 440 'twenty-twenty-one-ie11-polyfills', 441 wp_get_script_polyfill( 442 $wp_scripts, 443 array( 444 'Element.prototype.matches && Element.prototype.closest && window.NodeList && NodeList.prototype.forEach' => 'twenty-twenty-one-ie11-polyfills-asset', 445 ) 446 ) 447 ); 448 449 // Main navigation scripts. 450 if ( has_nav_menu( 'primary' ) ) { 451 wp_enqueue_script( 452 'twenty-twenty-one-primary-navigation-script', 453 get_template_directory_uri() . '/assets/js/primary-navigation.js', 454 array( 'twenty-twenty-one-ie11-polyfills' ), 455 wp_get_theme()->get( 'Version' ), 456 array( 457 'in_footer' => false, // Because involves header. 458 'strategy' => 'defer', 459 ) 460 ); 461 } 462 463 // Responsive embeds script. 464 wp_enqueue_script( 465 'twenty-twenty-one-responsive-embeds-script', 466 get_template_directory_uri() . '/assets/js/responsive-embeds.js', 467 array( 'twenty-twenty-one-ie11-polyfills' ), 468 wp_get_theme()->get( 'Version' ), 469 array( 'in_footer' => true ) 470 ); 471 } 472 add_action( 'wp_enqueue_scripts', 'twenty_twenty_one_scripts' ); 473 474 /** 475 * Enqueues block editor script. 476 * 477 * @since Twenty Twenty-One 1.0 478 * 479 * @return void 480 */ 481 function twentytwentyone_block_editor_script() { 482 483 wp_enqueue_script( 'twentytwentyone-editor', get_theme_file_uri( '/assets/js/editor.js' ), array( 'wp-blocks', 'wp-dom' ), wp_get_theme()->get( 'Version' ), array( 'in_footer' => true ) ); 484 } 485 486 add_action( 'enqueue_block_editor_assets', 'twentytwentyone_block_editor_script' ); 487 488 /** 489 * Fixes skip link focus in IE11. 490 * 491 * This does not enqueue the script because it is tiny and because it is only for IE11, 492 * thus it does not warrant having an entire dedicated blocking script being loaded. 493 * 494 * @since Twenty Twenty-One 1.0 495 * @deprecated Twenty Twenty-One 1.9 Removed from wp_print_footer_scripts action. 496 * 497 * @link https://git.io/vWdr2 498 */ 499 function twenty_twenty_one_skip_link_focus_fix() { 500 501 // If SCRIPT_DEBUG is defined and true, print the unminified file. 502 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { 503 echo '<script>'; 504 include get_template_directory() . '/assets/js/skip-link-focus-fix.js'; 505 echo '</script>'; 506 } else { 507 // The following is minified via `npx terser --compress --mangle -- assets/js/skip-link-focus-fix.js`. 508 ?> 509 <script> 510 /(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",(function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())}),!1); 511 </script> 512 <?php 513 } 514 } 515 516 /** 517 * Enqueues non-latin language styles. 518 * 519 * @since Twenty Twenty-One 1.0 520 * 521 * @return void 522 */ 523 function twenty_twenty_one_non_latin_languages() { 524 $custom_css = twenty_twenty_one_get_non_latin_css( 'front-end' ); 525 526 if ( $custom_css ) { 527 wp_add_inline_style( 'twenty-twenty-one-style', $custom_css ); 528 } 529 } 530 add_action( 'wp_enqueue_scripts', 'twenty_twenty_one_non_latin_languages' ); 531 532 // SVG Icons class. 533 require get_template_directory() . '/classes/class-twenty-twenty-one-svg-icons.php'; 534 535 // Custom color classes. 536 require get_template_directory() . '/classes/class-twenty-twenty-one-custom-colors.php'; 537 new Twenty_Twenty_One_Custom_Colors(); 538 539 // Enhance the theme by hooking into WordPress. 540 require get_template_directory() . '/inc/template-functions.php'; 541 542 // Menu functions and filters. 543 require get_template_directory() . '/inc/menu-functions.php'; 544 545 // Custom template tags for the theme. 546 require get_template_directory() . '/inc/template-tags.php'; 547 548 // Customizer additions. 549 require get_template_directory() . '/classes/class-twenty-twenty-one-customize.php'; 550 new Twenty_Twenty_One_Customize(); 551 552 // Block Patterns. 553 require get_template_directory() . '/inc/block-patterns.php'; 554 555 // Block Styles. 556 require get_template_directory() . '/inc/block-styles.php'; 557 558 // Dark Mode. 559 require_once get_template_directory() . '/classes/class-twenty-twenty-one-dark-mode.php'; 560 new Twenty_Twenty_One_Dark_Mode(); 561 562 /** 563 * Enqueues scripts for the customizer preview. 564 * 565 * @since Twenty Twenty-One 1.0 566 * 567 * @return void 568 */ 569 function twentytwentyone_customize_preview_init() { 570 wp_enqueue_script( 571 'twentytwentyone-customize-helpers', 572 get_theme_file_uri( '/assets/js/customize-helpers.js' ), 573 array(), 574 wp_get_theme()->get( 'Version' ), 575 array( 'in_footer' => true ) 576 ); 577 578 wp_enqueue_script( 579 'twentytwentyone-customize-preview', 580 get_theme_file_uri( '/assets/js/customize-preview.js' ), 581 array( 'customize-preview', 'customize-selective-refresh', 'jquery', 'twentytwentyone-customize-helpers' ), 582 wp_get_theme()->get( 'Version' ), 583 array( 'in_footer' => true ) 584 ); 585 } 586 add_action( 'customize_preview_init', 'twentytwentyone_customize_preview_init' ); 587 588 /** 589 * Enqueues scripts for the customizer. 590 * 591 * @since Twenty Twenty-One 1.0 592 * 593 * @return void 594 */ 595 function twentytwentyone_customize_controls_enqueue_scripts() { 596 597 wp_enqueue_script( 598 'twentytwentyone-customize-helpers', 599 get_theme_file_uri( '/assets/js/customize-helpers.js' ), 600 array(), 601 wp_get_theme()->get( 'Version' ), 602 array( 'in_footer' => true ) 603 ); 604 } 605 add_action( 'customize_controls_enqueue_scripts', 'twentytwentyone_customize_controls_enqueue_scripts' ); 606 607 /** 608 * Calculates classes for the main <html> element. 609 * 610 * @since Twenty Twenty-One 1.0 611 * 612 * @return void 613 */ 614 function twentytwentyone_the_html_classes() { 615 /** 616 * Filters the classes for the main <html> element. 617 * 618 * @since Twenty Twenty-One 1.0 619 * 620 * @param string The list of classes. Default empty string. 621 */ 622 $classes = apply_filters( 'twentytwentyone_html_classes', '' ); 623 if ( ! $classes ) { 624 return; 625 } 626 echo 'class="' . esc_attr( $classes ) . '"'; 627 } 628 629 /** 630 * Adds "is-IE" class to body if the user is on Internet Explorer. 631 * 632 * @since Twenty Twenty-One 1.0 633 * 634 * @return void 635 */ 636 function twentytwentyone_add_ie_class() { 637 $script = " 638 if ( -1 !== navigator.userAgent.indexOf('MSIE') || -1 !== navigator.appVersion.indexOf('Trident/') ) { 639 document.body.classList.add('is-IE'); 640 } 641 "; 642 $script .= '//# sourceURL=' . rawurlencode( __FUNCTION__ ); 643 644 if ( function_exists( 'wp_print_inline_script_tag' ) ) { 645 wp_print_inline_script_tag( $script ); 646 } else { 647 echo "<script>$script</script>\n"; 648 } 649 } 650 add_action( 'wp_footer', 'twentytwentyone_add_ie_class' ); 651 652 if ( ! function_exists( 'wp_get_list_item_separator' ) ) : 653 /** 654 * Retrieves the list item separator based on the locale. 655 * 656 * Added for backward compatibility to support pre-6.0.0 WordPress versions. 657 * 658 * @since 6.0.0 659 */ 660 function wp_get_list_item_separator() { 661 /* translators: Used between list items, there is a space after the comma. */ 662 return __( ', ', 'twentytwentyone' ); 663 } 664 endif;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Thu Oct 30 08:20:06 2025 | Cross-referenced by PHPXref |