[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/themes/twentytwentyone/ -> functions.php (source)

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


Generated : Tue Jun 30 08:20:12 2026 Cross-referenced by PHPXref