[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Functions which enhance the theme by hooking into WordPress
   4   *
   5   * @package WordPress
   6   * @subpackage Twenty_Twenty_One
   7   * @since Twenty Twenty-One 1.0
   8   */
   9  
  10  /**
  11   * Adds custom classes to the array of body classes.
  12   *
  13   * @since Twenty Twenty-One 1.0
  14   *
  15   * @param array $classes Classes for the body element.
  16   * @return array
  17   */
  18  function twenty_twenty_one_body_classes( $classes ) {
  19  
  20      // Helps detect if JS is enabled or not.
  21      $classes[] = 'no-js';
  22  
  23      // Adds `singular` to singular pages, and `hfeed` to all other pages.
  24      $classes[] = is_singular() ? 'singular' : 'hfeed';
  25  
  26      // Add a body class if main navigation is active.
  27      if ( has_nav_menu( 'primary' ) ) {
  28          $classes[] = 'has-main-navigation';
  29      }
  30  
  31      // Add a body class if there are no footer widgets.
  32      if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  33          $classes[] = 'no-widgets';
  34      }
  35  
  36      return $classes;
  37  }
  38  add_filter( 'body_class', 'twenty_twenty_one_body_classes' );
  39  
  40  /**
  41   * Adds custom class to the array of posts classes.
  42   *
  43   * @since Twenty Twenty-One 1.0
  44   *
  45   * @param array $classes An array of CSS classes.
  46   * @return array
  47   */
  48  function twenty_twenty_one_post_classes( $classes ) {
  49      $classes[] = 'entry';
  50  
  51      return $classes;
  52  }
  53  add_filter( 'post_class', 'twenty_twenty_one_post_classes', 10, 3 );
  54  
  55  /**
  56   * Adds a pingback url auto-discovery header for single posts, pages, or attachments.
  57   *
  58   * @since Twenty Twenty-One 1.0
  59   *
  60   * @return void
  61   */
  62  function twenty_twenty_one_pingback_header() {
  63      if ( is_singular() && pings_open() ) {
  64          echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  65      }
  66  }
  67  add_action( 'wp_head', 'twenty_twenty_one_pingback_header' );
  68  
  69  /**
  70   * Removes the `no-js` class from body if JS is supported.
  71   *
  72   * @since Twenty Twenty-One 1.0
  73   *
  74   * @return void
  75   */
  76  function twenty_twenty_one_supports_js() {
  77      $js  = "document.body.classList.remove('no-js');";
  78      $js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
  79  
  80      if ( function_exists( 'wp_print_inline_script_tag' ) ) {
  81          wp_print_inline_script_tag( $js );
  82      } else {
  83          echo "<script>$js</script>\n";
  84      }
  85  }
  86  add_action( 'wp_footer', 'twenty_twenty_one_supports_js' );
  87  
  88  /**
  89   * Changes comment form default fields.
  90   *
  91   * @since Twenty Twenty-One 1.0
  92   *
  93   * @param array $defaults The form defaults.
  94   * @return array
  95   */
  96  function twenty_twenty_one_comment_form_defaults( $defaults ) {
  97  
  98      // Adjust height of comment form.
  99      $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $defaults['comment_field'] );
 100  
 101      return $defaults;
 102  }
 103  add_filter( 'comment_form_defaults', 'twenty_twenty_one_comment_form_defaults' );
 104  
 105  /**
 106   * Determines if post thumbnail can be displayed.
 107   *
 108   * @since Twenty Twenty-One 1.0
 109   *
 110   * @return bool
 111   */
 112  function twenty_twenty_one_can_show_post_thumbnail() {
 113      /**
 114       * Filters whether post thumbnail can be displayed.
 115       *
 116       * @since Twenty Twenty-One 1.0
 117       *
 118       * @param bool $show_post_thumbnail Whether to show post thumbnail.
 119       */
 120      return apply_filters(
 121          'twenty_twenty_one_can_show_post_thumbnail',
 122          ! post_password_required() && ! is_attachment() && has_post_thumbnail()
 123      );
 124  }
 125  
 126  /**
 127   * Returns the size for avatars used in the theme.
 128   *
 129   * @since Twenty Twenty-One 1.0
 130   *
 131   * @return int
 132   */
 133  function twenty_twenty_one_get_avatar_size() {
 134      return 60;
 135  }
 136  
 137  /**
 138   * Creates continue reading text.
 139   *
 140   * @since Twenty Twenty-One 1.0
 141   */
 142  function twenty_twenty_one_continue_reading_text() {
 143      $continue_reading = sprintf(
 144          /* translators: %s: Post title. Only visible to screen readers. */
 145          esc_html__( 'Continue reading %s', 'twentytwentyone' ),
 146          the_title( '<span class="screen-reader-text">', '</span>', false )
 147      );
 148  
 149      return $continue_reading;
 150  }
 151  
 152  /**
 153   * Creates the continue reading link for excerpt.
 154   *
 155   * @since Twenty Twenty-One 1.0
 156   */
 157  function twenty_twenty_one_continue_reading_link_excerpt() {
 158      if ( ! is_admin() ) {
 159          return '&hellip; <a class="more-link" href="' . esc_url( get_permalink() ) . '">' . twenty_twenty_one_continue_reading_text() . '</a>';
 160      }
 161  }
 162  
 163  // Filter the excerpt more link.
 164  add_filter( 'excerpt_more', 'twenty_twenty_one_continue_reading_link_excerpt' );
 165  
 166  /**
 167   * Creates the continue reading link.
 168   *
 169   * @since Twenty Twenty-One 1.0
 170   */
 171  function twenty_twenty_one_continue_reading_link() {
 172      if ( ! is_admin() ) {
 173          return '<div class="more-link-container"><a class="more-link" href="' . esc_url( get_permalink() ) . '#more-' . esc_attr( get_the_ID() ) . '">' . twenty_twenty_one_continue_reading_text() . '</a></div>';
 174      }
 175  }
 176  
 177  // Filter the content more link.
 178  add_filter( 'the_content_more_link', 'twenty_twenty_one_continue_reading_link' );
 179  
 180  if ( ! function_exists( 'twenty_twenty_one_post_title' ) ) {
 181      /**
 182       * Adds a title to posts and pages that are missing titles.
 183       *
 184       * @since Twenty Twenty-One 1.0
 185       *
 186       * @param string $title The title.
 187       * @return string
 188       */
 189  	function twenty_twenty_one_post_title( $title ) {
 190          return '' === $title ? esc_html_x( 'Untitled', 'Added to posts and pages that are missing titles', 'twentytwentyone' ) : $title;
 191      }
 192  }
 193  add_filter( 'the_title', 'twenty_twenty_one_post_title' );
 194  
 195  /**
 196   * Gets the SVG code for a given icon.
 197   *
 198   * @since Twenty Twenty-One 1.0
 199   *
 200   * @param string $group The icon group.
 201   * @param string $icon  The icon.
 202   * @param int    $size  The icon size in pixels.
 203   * @return string
 204   */
 205  function twenty_twenty_one_get_icon_svg( $group, $icon, $size = 24 ) {
 206      return Twenty_Twenty_One_SVG_Icons::get_svg( $group, $icon, $size );
 207  }
 208  
 209  /**
 210   * Changes the default navigation arrows to svg icons
 211   *
 212   * @since Twenty Twenty-One 1.0
 213   *
 214   * @param string $calendar_output The generated HTML of the calendar.
 215   * @return string
 216   */
 217  function twenty_twenty_one_change_calendar_nav_arrows( $calendar_output ) {
 218      $calendar_output = str_replace( '&laquo; ', is_rtl() ? twenty_twenty_one_get_icon_svg( 'ui', 'arrow_right' ) : twenty_twenty_one_get_icon_svg( 'ui', 'arrow_left' ), $calendar_output );
 219      $calendar_output = str_replace( ' &raquo;', is_rtl() ? twenty_twenty_one_get_icon_svg( 'ui', 'arrow_left' ) : twenty_twenty_one_get_icon_svg( 'ui', 'arrow_right' ), $calendar_output );
 220      return $calendar_output;
 221  }
 222  add_filter( 'get_calendar', 'twenty_twenty_one_change_calendar_nav_arrows' );
 223  
 224  /**
 225   * Gets custom CSS.
 226   *
 227   * Return CSS for non-latin language, if available, or null
 228   *
 229   * @since Twenty Twenty-One 1.0
 230   *
 231   * @param string $type Whether to return CSS for the "front-end", "block-editor", or "classic-editor".
 232   * @return string
 233   */
 234  function twenty_twenty_one_get_non_latin_css( $type = 'front-end' ) {
 235  
 236      // Fetch site locale.
 237      $locale = get_bloginfo( 'language' );
 238  
 239      /**
 240       * Filters the fallback fonts for non-latin languages.
 241       *
 242       * @since Twenty Twenty-One 1.0
 243       *
 244       * @param array $font_family An array of locales and font families.
 245       */
 246      $font_family = apply_filters(
 247          'twenty_twenty_one_get_localized_font_family_types',
 248          array(
 249  
 250              // Arabic.
 251              'ar'    => array( 'Tahoma', 'Arial', 'sans-serif' ),
 252              'ary'   => array( 'Tahoma', 'Arial', 'sans-serif' ),
 253              'azb'   => array( 'Tahoma', 'Arial', 'sans-serif' ),
 254              'ckb'   => array( 'Tahoma', 'Arial', 'sans-serif' ),
 255              'fa-IR' => array( 'Tahoma', 'Arial', 'sans-serif' ),
 256              'haz'   => array( 'Tahoma', 'Arial', 'sans-serif' ),
 257              'ps'    => array( 'Tahoma', 'Arial', 'sans-serif' ),
 258  
 259              // Chinese Simplified (China) - Noto Sans SC.
 260              'zh-CN' => array( '\'PingFang SC\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ),
 261  
 262              // Chinese Traditional (Taiwan) - Noto Sans TC.
 263              'zh-TW' => array( '\'PingFang TC\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ),
 264  
 265              // Chinese (Hong Kong) - Noto Sans HK.
 266              'zh-HK' => array( '\'PingFang HK\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ),
 267  
 268              // Cyrillic.
 269              'bel'   => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 270              'bg-BG' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 271              'kk'    => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 272              'mk-MK' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 273              'mn'    => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 274              'ru-RU' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 275              'sah'   => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 276              'sr-RS' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 277              'tt-RU' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 278              'uk'    => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 279  
 280              // Devanagari.
 281              'bn-BD' => array( 'Arial', 'sans-serif' ),
 282              'hi-IN' => array( 'Arial', 'sans-serif' ),
 283              'mr'    => array( 'Arial', 'sans-serif' ),
 284              'ne-NP' => array( 'Arial', 'sans-serif' ),
 285  
 286              // Greek.
 287              'el'    => array( '\'Helvetica Neue\', Helvetica, Arial, sans-serif' ),
 288  
 289              // Gujarati.
 290              'gu'    => array( 'Arial', 'sans-serif' ),
 291  
 292              // Hebrew.
 293              'he-IL' => array( '\'Arial Hebrew\'', 'Arial', 'sans-serif' ),
 294  
 295              // Japanese.
 296              'ja'    => array( 'sans-serif' ),
 297  
 298              // Korean.
 299              'ko-KR' => array( '\'Apple SD Gothic Neo\'', '\'Malgun Gothic\'', '\'Nanum Gothic\'', 'Dotum', 'sans-serif' ),
 300  
 301              // Thai.
 302              'th'    => array( '\'Sukhumvit Set\'', '\'Helvetica Neue\'', 'Helvetica', 'Arial', 'sans-serif' ),
 303  
 304              // Vietnamese.
 305              'vi'    => array( '\'Libre Franklin\'', 'sans-serif' ),
 306  
 307          )
 308      );
 309  
 310      // Return if the selected language has no fallback fonts.
 311      if ( empty( $font_family[ $locale ] ) ) {
 312          return '';
 313      }
 314  
 315      /**
 316       * Filters the elements to apply fallback fonts to.
 317       *
 318       * @since Twenty Twenty-One 1.0
 319       *
 320       * @param array $elements An array of elements for "front-end", "block-editor", or "classic-editor".
 321       */
 322      $elements = apply_filters(
 323          'twenty_twenty_one_get_localized_font_family_elements',
 324          array(
 325              'front-end'      => array( 'body', 'input', 'textarea', 'button', '.button', '.faux-button', '.wp-block-button__link', '.wp-block-file__button', '.has-drop-cap:not(:focus)::first-letter', '.entry-content .wp-block-archives', '.entry-content .wp-block-categories', '.entry-content .wp-block-cover-image', '.entry-content .wp-block-latest-comments', '.entry-content .wp-block-latest-posts', '.entry-content .wp-block-pullquote', '.entry-content .wp-block-quote.is-large', '.entry-content .wp-block-quote.is-style-large', '.entry-content .wp-block-archives *', '.entry-content .wp-block-categories *', '.entry-content .wp-block-latest-posts *', '.entry-content .wp-block-latest-comments *', '.entry-content p', '.entry-content ol', '.entry-content ul', '.entry-content dl', '.entry-content dt', '.entry-content cite', '.entry-content figcaption', '.entry-content .wp-caption-text', '.comment-content p', '.comment-content ol', '.comment-content ul', '.comment-content dl', '.comment-content dt', '.comment-content cite', '.comment-content figcaption', '.comment-content .wp-caption-text', '.widget_text p', '.widget_text ol', '.widget_text ul', '.widget_text dl', '.widget_text dt', '.widget-content .rssSummary', '.widget-content cite', '.widget-content figcaption', '.widget-content .wp-caption-text' ),
 326              'block-editor'   => array( '.editor-styles-wrapper > *', '.editor-styles-wrapper p', '.editor-styles-wrapper ol', '.editor-styles-wrapper ul', '.editor-styles-wrapper dl', '.editor-styles-wrapper dt', '.editor-post-title__block .editor-post-title__input', '.editor-styles-wrapper .wp-block h1', '.editor-styles-wrapper .wp-block h2', '.editor-styles-wrapper .wp-block h3', '.editor-styles-wrapper .wp-block h4', '.editor-styles-wrapper .wp-block h5', '.editor-styles-wrapper .wp-block h6', '.editor-styles-wrapper .has-drop-cap:not(:focus)::first-letter', '.editor-styles-wrapper cite', '.editor-styles-wrapper figcaption', '.editor-styles-wrapper .wp-caption-text' ),
 327              'classic-editor' => array( 'body#tinymce.wp-editor', 'body#tinymce.wp-editor p', 'body#tinymce.wp-editor ol', 'body#tinymce.wp-editor ul', 'body#tinymce.wp-editor dl', 'body#tinymce.wp-editor dt', 'body#tinymce.wp-editor figcaption', 'body#tinymce.wp-editor .wp-caption-text', 'body#tinymce.wp-editor .wp-caption-dd', 'body#tinymce.wp-editor cite', 'body#tinymce.wp-editor table' ),
 328          )
 329      );
 330  
 331      // Return if the specified type doesn't exist.
 332      if ( empty( $elements[ $type ] ) ) {
 333          return '';
 334      }
 335  
 336      // Include file if function doesn't exist.
 337      if ( ! function_exists( 'twenty_twenty_one_generate_css' ) ) {
 338          require_once get_theme_file_path( 'inc/custom-css.php' ); // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound
 339      }
 340  
 341      // Return the specified styles.
 342      return twenty_twenty_one_generate_css( // @phpstan-ignore-line.
 343          implode( ',', $elements[ $type ] ),
 344          'font-family',
 345          implode( ',', $font_family[ $locale ] ),
 346          null,
 347          null,
 348          false
 349      );
 350  }
 351  
 352  /**
 353   * Prints the first instance of a block in the content, and then break away.
 354   *
 355   * @since Twenty Twenty-One 1.0
 356   *
 357   * @param string      $block_name The full block type name, or a partial match.
 358   *                                Example: `core/image`, `core-embed/*`.
 359   * @param string|null $content    The content to search in. Use null for get_the_content().
 360   * @param int         $instances  How many instances of the block will be printed (max). Default  1.
 361   * @return bool Returns true if a block was located & printed, otherwise false.
 362   */
 363  function twenty_twenty_one_print_first_instance_of_block( $block_name, $content = null, $instances = 1 ) {
 364      $instances_count = 0;
 365      $blocks_content  = '';
 366  
 367      if ( ! $content ) {
 368          $content = get_the_content();
 369      }
 370  
 371      // Parse blocks in the content.
 372      $blocks = parse_blocks( $content );
 373  
 374      // Loop blocks.
 375      foreach ( $blocks as $block ) {
 376  
 377          // Confidence check.
 378          if ( ! isset( $block['blockName'] ) ) {
 379              continue;
 380          }
 381  
 382          // Check if this the block matches the $block_name.
 383          $is_matching_block = false;
 384  
 385          // If the block ends with *, try to match the first portion.
 386          if ( '*' === $block_name[-1] ) {
 387              $is_matching_block = 0 === strpos( $block['blockName'], rtrim( $block_name, '*' ) );
 388          } else {
 389              $is_matching_block = $block_name === $block['blockName'];
 390          }
 391  
 392          if ( $is_matching_block ) {
 393              // Increment count.
 394              ++$instances_count;
 395  
 396              // Add the block HTML.
 397              $blocks_content .= render_block( $block );
 398  
 399              // Break the loop if the $instances count was reached.
 400              if ( $instances_count >= $instances ) {
 401                  break;
 402              }
 403          }
 404      }
 405  
 406      if ( $blocks_content ) {
 407          /** This filter is documented in wp-includes/post-template.php */
 408          echo apply_filters( 'the_content', $blocks_content ); // phpcs:ignore WordPress.Security.EscapeOutput
 409          return true;
 410      }
 411  
 412      return false;
 413  }
 414  
 415  /**
 416   * Retrieves protected post password form content.
 417   *
 418   * @since Twenty Twenty-One 1.0
 419   * @since Twenty Twenty-One 1.4 Corrected parameter name for `$output`,
 420   *                              added the `$post` parameter.
 421   *
 422   * @param string      $output The password form HTML output.
 423   * @param int|WP_Post $post   Optional. Post ID or WP_Post object. Default is global $post.
 424   * @return string HTML content for password form for password protected post.
 425   */
 426  function twenty_twenty_one_password_form( $output, $post = 0 ) {
 427      $post   = get_post( $post );
 428      $label  = 'pwbox-' . ( empty( $post->ID ) ? wp_rand() : $post->ID );
 429      $output = '<p class="post-password-message">' . esc_html__( 'This content is password protected. Please enter a password to view.', 'twentytwentyone' ) . '</p>
 430      <form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">
 431      <label class="post-password-form__label" for="' . esc_attr( $label ) . '">' . esc_html_x( 'Password', 'Post password form', 'twentytwentyone' ) . '</label><input class="post-password-form__input" name="post_password" id="' . esc_attr( $label ) . '" type="password" spellcheck="false" size="20" /><input type="submit" class="post-password-form__submit" name="' . esc_attr_x( 'Submit', 'Post password form', 'twentytwentyone' ) . '" value="' . esc_attr_x( 'Enter', 'Post password form', 'twentytwentyone' ) . '" /></form>
 432      ';
 433      return $output;
 434  }
 435  add_filter( 'the_password_form', 'twenty_twenty_one_password_form', 10, 2 );
 436  
 437  /**
 438   * Filters the list of attachment image attributes.
 439   *
 440   * @since Twenty Twenty-One 1.0
 441   *
 442   * @param string[]     $attr       Array of attribute values for the image markup, keyed by attribute name.
 443   *                                 See wp_get_attachment_image().
 444   * @param WP_Post      $attachment Image attachment post.
 445   * @param string|int[] $size       Requested image size. Can be any registered image size name, or
 446   *                                 an array of width and height values in pixels (in that order).
 447   * @return string[] The filtered attributes for the image markup.
 448   */
 449  function twenty_twenty_one_get_attachment_image_attributes( $attr, $attachment, $size ) {
 450  
 451      if ( is_admin() ) {
 452          return $attr;
 453      }
 454  
 455      if ( isset( $attr['class'] ) && false !== strpos( $attr['class'], 'custom-logo' ) ) {
 456          return $attr;
 457      }
 458  
 459      $width  = false;
 460      $height = false;
 461  
 462      if ( is_array( $size ) ) {
 463          $width  = (int) $size[0];
 464          $height = (int) $size[1];
 465      } elseif ( $attachment && is_object( $attachment ) && $attachment->ID ) {
 466          $meta = wp_get_attachment_metadata( $attachment->ID );
 467          if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) {
 468              $width  = (int) $meta['width'];
 469              $height = (int) $meta['height'];
 470          }
 471      }
 472  
 473      if ( $width && $height ) {
 474  
 475          // Add style.
 476          $attr['style'] = isset( $attr['style'] ) ? $attr['style'] : '';
 477          $attr['style'] = 'width:100%;height:' . round( 100 * $height / $width, 2 ) . '%;max-width:' . $width . 'px;' . $attr['style'];
 478      }
 479  
 480      return $attr;
 481  }
 482  add_filter( 'wp_get_attachment_image_attributes', 'twenty_twenty_one_get_attachment_image_attributes', 10, 3 );


Generated : Fri Oct 10 08:20:03 2025 Cross-referenced by PHPXref