[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/themes/twentynineteen/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_Nineteen
   7   * @since Twenty Nineteen 1.0
   8   */
   9  
  10  /**
  11   * Adds custom classes to the array of body classes.
  12   *
  13   * @param array $classes Classes for the body element.
  14   * @return array
  15   */
  16  function twentynineteen_body_classes( $classes ) {
  17  
  18      if ( is_singular() ) {
  19          // Adds `singular` to singular pages.
  20          $classes[] = 'singular';
  21      } else {
  22          // Adds `hfeed` to non-singular pages.
  23          $classes[] = 'hfeed';
  24      }
  25  
  26      // Adds a class if image filters are enabled.
  27      if ( twentynineteen_image_filters_enabled() ) {
  28          $classes[] = 'image-filters-enabled';
  29      }
  30  
  31      return $classes;
  32  }
  33  add_filter( 'body_class', 'twentynineteen_body_classes' );
  34  
  35  /**
  36   * Adds custom class to the array of posts classes.
  37   *
  38   * @param array $classes A list of existing post class values.
  39   * @return array The filtered post class list.
  40   */
  41  function twentynineteen_post_classes( $classes ) {
  42      $classes[] = 'entry';
  43  
  44      return $classes;
  45  }
  46  add_filter( 'post_class', 'twentynineteen_post_classes' );
  47  
  48  /**
  49   * Adds a pingback url auto-discovery header for single posts, pages, or attachments.
  50   */
  51  function twentynineteen_pingback_header() {
  52      if ( is_singular() && pings_open() ) {
  53          echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  54      }
  55  }
  56  add_action( 'wp_head', 'twentynineteen_pingback_header' );
  57  
  58  /**
  59   * Changes comment form default fields.
  60   */
  61  function twentynineteen_comment_form_defaults( $defaults ) {
  62      $comment_field = $defaults['comment_field'];
  63  
  64      // Adjust height of comment form.
  65      $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $comment_field );
  66  
  67      return $defaults;
  68  }
  69  add_filter( 'comment_form_defaults', 'twentynineteen_comment_form_defaults' );
  70  
  71  /**
  72   * Filters the default archive titles.
  73   */
  74  function twentynineteen_get_the_archive_title() {
  75      if ( is_category() ) {
  76          $title = __( 'Category Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  77      } elseif ( is_tag() ) {
  78          $title = __( 'Tag Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  79      } elseif ( is_author() ) {
  80          $title = __( 'Author Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>';
  81      } elseif ( is_year() ) {
  82          $title = __( 'Yearly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'twentynineteen' ) ) . '</span>';
  83      } elseif ( is_month() ) {
  84          $title = __( 'Monthly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'twentynineteen' ) ) . '</span>';
  85      } elseif ( is_day() ) {
  86          $title = __( 'Daily Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date() . '</span>';
  87      } elseif ( is_post_type_archive() ) {
  88          $title = __( 'Post Type Archives: ', 'twentynineteen' ) . '<span class="page-description">' . post_type_archive_title( '', false ) . '</span>';
  89      } elseif ( is_tax() ) {
  90          $tax = get_taxonomy( get_queried_object()->taxonomy );
  91          /* translators: %s: Taxonomy singular name. */
  92          $title = sprintf( esc_html__( '%s Archives:', 'twentynineteen' ), $tax->labels->singular_name );
  93      } else {
  94          $title = __( 'Archives:', 'twentynineteen' );
  95      }
  96      return $title;
  97  }
  98  add_filter( 'get_the_archive_title', 'twentynineteen_get_the_archive_title' );
  99  
 100  /**
 101   * Adds custom 'sizes' attribute to responsive image functionality for post thumbnails.
 102   *
 103   * @since Twenty Nineteen 1.0
 104   *
 105   * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
 106   *                       See wp_get_attachment_image().
 107   * @return string[] The filtered attributes for the image markup.
 108   */
 109  function twentynineteen_post_thumbnail_sizes_attr( $attr ) {
 110  
 111      if ( is_admin() ) {
 112          return $attr;
 113      }
 114  
 115      if ( ! is_singular() ) {
 116          $attr['sizes'] = '(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw';
 117      }
 118  
 119      return $attr;
 120  }
 121  add_filter( 'wp_get_attachment_image_attributes', 'twentynineteen_post_thumbnail_sizes_attr' );
 122  
 123  /**
 124   * Adds an extra menu to our nav for our priority+ navigation to use.
 125   *
 126   * @param string $nav_menu Nav menu.
 127   * @param object $args     Nav menu args.
 128   * @return string More link for hidden menu items.
 129   */
 130  function twentynineteen_add_ellipses_to_nav( $nav_menu, $args ) {
 131  
 132      if ( 'menu-1' === $args->theme_location ) :
 133  
 134          $nav_menu .= '
 135              <div class="main-menu-more">
 136                  <ul class="main-menu">
 137                      <li class="menu-item menu-item-has-children">
 138                          <button class="submenu-expand main-menu-more-toggle is-empty" tabindex="-1"
 139                              aria-label="' . esc_attr__( 'More', 'twentynineteen' ) . '" aria-haspopup="true" aria-expanded="false">' .
 140                              twentynineteen_get_icon_svg( 'arrow_drop_down_ellipsis' ) . '
 141                          </button>
 142                          <ul class="sub-menu hidden-links">
 143                              <li class="mobile-parent-nav-menu-item">
 144                                  <button class="menu-item-link-return">' .
 145                                      twentynineteen_get_icon_svg( 'chevron_left' ) .
 146                                      esc_html__( 'Back', 'twentynineteen' ) . '
 147                                  </button>
 148                              </li>
 149                          </ul>
 150                      </li>
 151                  </ul>
 152              </div>';
 153  
 154      endif;
 155  
 156      return $nav_menu;
 157  }
 158  add_filter( 'wp_nav_menu', 'twentynineteen_add_ellipses_to_nav', 10, 2 );
 159  
 160  /**
 161   * Handles WCAG 2.0 attributes for dropdown menus.
 162   *
 163   * Adjustments to menu attributes to support WCAG 2.0 recommendations
 164   * for flyout and dropdown menus.
 165   *
 166   * @link https://www.w3.org/WAI/tutorials/menus/flyout/
 167   *
 168   * @param array   $atts {
 169   *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
 170   *
 171   *     @type string $title        Title attribute.
 172   *     @type string $target       Target attribute.
 173   *     @type string $rel          The rel attribute.
 174   *     @type string $href         The href attribute.
 175   *     @type string $aria-current The aria-current attribute.
 176   * }
 177   * @param WP_Post $item The current menu item object.
 178   * @return string[] Modified attributes.
 179   */
 180  function twentynineteen_nav_menu_link_attributes( $atts, $item ) {
 181  
 182      // Add [aria-haspopup] and [aria-expanded] to menu items that have children.
 183      $item_has_children = in_array( 'menu-item-has-children', $item->classes, true );
 184      if ( $item_has_children ) {
 185          $atts['aria-haspopup'] = 'true';
 186          $atts['aria-expanded'] = 'false';
 187      }
 188  
 189      return $atts;
 190  }
 191  add_filter( 'nav_menu_link_attributes', 'twentynineteen_nav_menu_link_attributes', 10, 2 );
 192  
 193  /**
 194   * Creates a nav menu item to be displayed on mobile to navigate from submenu back to the parent.
 195   *
 196   * This duplicates each parent nav menu item and makes it the first child of itself.
 197   *
 198   * @param array  $sorted_menu_items Sorted nav menu items.
 199   * @param object $args              Nav menu args.
 200   * @return array Amended nav menu items.
 201   */
 202  function twentynineteen_add_mobile_parent_nav_menu_items( $sorted_menu_items, $args ) {
 203      static $pseudo_id = 0;
 204      if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
 205          return $sorted_menu_items;
 206      }
 207  
 208      $amended_menu_items = array();
 209      foreach ( $sorted_menu_items as $nav_menu_item ) {
 210          $amended_menu_items[] = $nav_menu_item;
 211          if ( in_array( 'menu-item-has-children', $nav_menu_item->classes, true ) ) {
 212              $parent_menu_item                   = clone $nav_menu_item;
 213              $parent_menu_item->original_id      = $nav_menu_item->ID;
 214              $parent_menu_item->ID               = --$pseudo_id;
 215              $parent_menu_item->db_id            = $parent_menu_item->ID;
 216              $parent_menu_item->object_id        = $parent_menu_item->ID;
 217              $parent_menu_item->classes          = array( 'mobile-parent-nav-menu-item' );
 218              $parent_menu_item->menu_item_parent = $nav_menu_item->ID;
 219  
 220              $amended_menu_items[] = $parent_menu_item;
 221          }
 222      }
 223  
 224      return $amended_menu_items;
 225  }
 226  add_filter( 'wp_nav_menu_objects', 'twentynineteen_add_mobile_parent_nav_menu_items', 10, 2 );
 227  
 228  /**
 229   * Adds a fragment identifier (to the content) to paginated links.
 230   *
 231   * @since Twenty Nineteen 2.6
 232   *
 233   * @param string $link The page number HTML output.
 234   * @param int    $i    Page number for paginated posts' page links.
 235   * @return string Formatted output in HTML.
 236   */
 237  function twentynineteen_link_pages_link( $link, $i ) {
 238      if ( $i > 1 && preg_match( '/href="([^"]*)"/', $link, $matches ) ) {
 239          $link = str_replace( $matches[1], $matches[1] . '#content', $link );
 240      }
 241      return $link;
 242  }
 243  add_filter( 'wp_link_pages_link', 'twentynineteen_link_pages_link', 10, 2 );


Generated : Tue Mar 19 08:20:01 2024 Cross-referenced by PHPXref