[ Index ]

PHP Cross Reference of WordPress

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  function twentynineteen_post_classes( $classes, $css_class, $post_id ) {
  39      $classes[] = 'entry';
  40  
  41      return $classes;
  42  }
  43  add_filter( 'post_class', 'twentynineteen_post_classes', 10, 3 );
  44  
  45  
  46  /**
  47   * Add a pingback url auto-discovery header for single posts, pages, or attachments.
  48   */
  49  function twentynineteen_pingback_header() {
  50      if ( is_singular() && pings_open() ) {
  51          echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  52      }
  53  }
  54  add_action( 'wp_head', 'twentynineteen_pingback_header' );
  55  
  56  /**
  57   * Changes comment form default fields.
  58   */
  59  function twentynineteen_comment_form_defaults( $defaults ) {
  60      $comment_field = $defaults['comment_field'];
  61  
  62      // Adjust height of comment form.
  63      $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $comment_field );
  64  
  65      return $defaults;
  66  }
  67  add_filter( 'comment_form_defaults', 'twentynineteen_comment_form_defaults' );
  68  
  69  /**
  70   * Filters the default archive titles.
  71   */
  72  function twentynineteen_get_the_archive_title() {
  73      if ( is_category() ) {
  74          $title = __( 'Category Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  75      } elseif ( is_tag() ) {
  76          $title = __( 'Tag Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  77      } elseif ( is_author() ) {
  78          $title = __( 'Author Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>';
  79      } elseif ( is_year() ) {
  80          $title = __( 'Yearly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'twentynineteen' ) ) . '</span>';
  81      } elseif ( is_month() ) {
  82          $title = __( 'Monthly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'twentynineteen' ) ) . '</span>';
  83      } elseif ( is_day() ) {
  84          $title = __( 'Daily Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date() . '</span>';
  85      } elseif ( is_post_type_archive() ) {
  86          $title = __( 'Post Type Archives: ', 'twentynineteen' ) . '<span class="page-description">' . post_type_archive_title( '', false ) . '</span>';
  87      } elseif ( is_tax() ) {
  88          $tax = get_taxonomy( get_queried_object()->taxonomy );
  89          /* translators: %s: Taxonomy singular name. */
  90          $title = sprintf( esc_html__( '%s Archives:', 'twentynineteen' ), $tax->labels->singular_name );
  91      } else {
  92          $title = __( 'Archives:', 'twentynineteen' );
  93      }
  94      return $title;
  95  }
  96  add_filter( 'get_the_archive_title', 'twentynineteen_get_the_archive_title' );
  97  
  98  /**
  99   * Add custom 'sizes' attribute to responsive image functionality for post thumbnails.
 100   *
 101   * @origin Twenty Nineteen 1.0
 102   *
 103   * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
 104   *                       See wp_get_attachment_image().
 105   * @return string[] The filtered attributes for the image markup.
 106   */
 107  function twentynineteen_post_thumbnail_sizes_attr( $attr ) {
 108  
 109      if ( is_admin() ) {
 110          return $attr;
 111      }
 112  
 113      if ( ! is_singular() ) {
 114          $attr['sizes'] = '(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw';
 115      }
 116  
 117      return $attr;
 118  }
 119  add_filter( 'wp_get_attachment_image_attributes', 'twentynineteen_post_thumbnail_sizes_attr', 10, 1 );
 120  
 121  /**
 122   * Add an extra menu to our nav for our priority+ navigation to use
 123   *
 124   * @param string $nav_menu  Nav menu.
 125   * @param object $args      Nav menu args.
 126   * @return string More link for hidden menu items.
 127   */
 128  function twentynineteen_add_ellipses_to_nav( $nav_menu, $args ) {
 129  
 130      if ( 'menu-1' === $args->theme_location ) :
 131  
 132          $nav_menu .= '
 133              <div class="main-menu-more">
 134                  <ul class="main-menu">
 135                      <li class="menu-item menu-item-has-children">
 136                          <button class="submenu-expand main-menu-more-toggle is-empty" tabindex="-1"
 137                              aria-label="' . esc_attr__( 'More', 'twentynineteen' ) . '" aria-haspopup="true" aria-expanded="false">' .
 138                              twentynineteen_get_icon_svg( 'arrow_drop_down_ellipsis' ) . '
 139                          </button>
 140                          <ul class="sub-menu hidden-links">
 141                              <li class="mobile-parent-nav-menu-item">
 142                                  <button class="menu-item-link-return">' .
 143                                      twentynineteen_get_icon_svg( 'chevron_left' ) .
 144                                      esc_html__( 'Back', 'twentynineteen' ) . '
 145                                  </button>
 146                              </li>
 147                          </ul>
 148                      </li>
 149                  </ul>
 150              </div>';
 151  
 152      endif;
 153  
 154      return $nav_menu;
 155  }
 156  add_filter( 'wp_nav_menu', 'twentynineteen_add_ellipses_to_nav', 10, 2 );
 157  
 158  /**
 159   * WCAG 2.0 Attributes for Dropdown Menus
 160   *
 161   * Adjustments to menu attributes tot support WCAG 2.0 recommendations
 162   * for flyout and dropdown menus.
 163   *
 164   * @ref https://www.w3.org/WAI/tutorials/menus/flyout/
 165   */
 166  function twentynineteen_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
 167  
 168      // Add [aria-haspopup] and [aria-expanded] to menu items that have children.
 169      $item_has_children = in_array( 'menu-item-has-children', $item->classes, true );
 170      if ( $item_has_children ) {
 171          $atts['aria-haspopup'] = 'true';
 172          $atts['aria-expanded'] = 'false';
 173      }
 174  
 175      return $atts;
 176  }
 177  add_filter( 'nav_menu_link_attributes', 'twentynineteen_nav_menu_link_attributes', 10, 4 );
 178  
 179  /**
 180   * Create a nav menu item to be displayed on mobile to navigate from submenu back to the parent.
 181   *
 182   * This duplicates each parent nav menu item and makes it the first child of itself.
 183   *
 184   * @param array  $sorted_menu_items Sorted nav menu items.
 185   * @param object $args              Nav menu args.
 186   * @return array Amended nav menu items.
 187   */
 188  function twentynineteen_add_mobile_parent_nav_menu_items( $sorted_menu_items, $args ) {
 189      static $pseudo_id = 0;
 190      if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
 191          return $sorted_menu_items;
 192      }
 193  
 194      $amended_menu_items = array();
 195      foreach ( $sorted_menu_items as $nav_menu_item ) {
 196          $amended_menu_items[] = $nav_menu_item;
 197          if ( in_array( 'menu-item-has-children', $nav_menu_item->classes, true ) ) {
 198              $parent_menu_item                   = clone $nav_menu_item;
 199              $parent_menu_item->original_id      = $nav_menu_item->ID;
 200              $parent_menu_item->ID               = --$pseudo_id;
 201              $parent_menu_item->db_id            = $parent_menu_item->ID;
 202              $parent_menu_item->object_id        = $parent_menu_item->ID;
 203              $parent_menu_item->classes          = array( 'mobile-parent-nav-menu-item' );
 204              $parent_menu_item->menu_item_parent = $nav_menu_item->ID;
 205  
 206              $amended_menu_items[] = $parent_menu_item;
 207          }
 208      }
 209  
 210      return $amended_menu_items;
 211  }
 212  add_filter( 'wp_nav_menu_objects', 'twentynineteen_add_mobile_parent_nav_menu_items', 10, 2 );


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1