[ 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  function twentynineteen_post_classes( $classes, $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 array $attr  Attributes for the image markup.
 104   * @return string Value for use in post thumbnail 'sizes' attribute.
 105   */
 106  function twentynineteen_post_thumbnail_sizes_attr( $attr ) {
 107  
 108      if ( is_admin() ) {
 109          return $attr;
 110      }
 111  
 112      if ( ! is_singular() ) {
 113          $attr['sizes'] = '(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw';
 114      }
 115  
 116      return $attr;
 117  }
 118  add_filter( 'wp_get_attachment_image_attributes', 'twentynineteen_post_thumbnail_sizes_attr', 10, 1 );
 119  
 120  /**
 121   * Add an extra menu to our nav for our priority+ navigation to use
 122   *
 123   * @param string $nav_menu  Nav menu.
 124   * @param object $args      Nav menu args.
 125   * @return string More link for hidden menu items.
 126   */
 127  function twentynineteen_add_ellipses_to_nav( $nav_menu, $args ) {
 128  
 129      if ( 'menu-1' === $args->theme_location ) :
 130  
 131          $nav_menu .= '
 132              <div class="main-menu-more">
 133                  <ul class="main-menu">
 134                      <li class="menu-item menu-item-has-children">
 135                          <button class="submenu-expand main-menu-more-toggle is-empty" tabindex="-1"
 136                              aria-label="' . esc_attr__( 'More', 'twentynineteen' ) . '" aria-haspopup="true" aria-expanded="false">' .
 137                              twentynineteen_get_icon_svg( 'arrow_drop_down_ellipsis' ) . '
 138                          </button>
 139                          <ul class="sub-menu hidden-links">
 140                              <li class="mobile-parent-nav-menu-item">
 141                                  <button class="menu-item-link-return">' .
 142                                      twentynineteen_get_icon_svg( 'chevron_left' ) .
 143                                      esc_html__( 'Back', 'twentynineteen' ) . '
 144                                  </button>
 145                              </li>
 146                          </ul>
 147                      </li>
 148                  </ul>
 149              </div>';
 150  
 151      endif;
 152  
 153      return $nav_menu;
 154  }
 155  add_filter( 'wp_nav_menu', 'twentynineteen_add_ellipses_to_nav', 10, 2 );
 156  
 157  /**
 158   * WCAG 2.0 Attributes for Dropdown Menus
 159   *
 160   * Adjustments to menu attributes tot support WCAG 2.0 recommendations
 161   * for flyout and dropdown menus.
 162   *
 163   * @ref https://www.w3.org/WAI/tutorials/menus/flyout/
 164   */
 165  function twentynineteen_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
 166  
 167      // Add [aria-haspopup] and [aria-expanded] to menu items that have children.
 168      $item_has_children = in_array( 'menu-item-has-children', $item->classes, true );
 169      if ( $item_has_children ) {
 170          $atts['aria-haspopup'] = 'true';
 171          $atts['aria-expanded'] = 'false';
 172      }
 173  
 174      return $atts;
 175  }
 176  add_filter( 'nav_menu_link_attributes', 'twentynineteen_nav_menu_link_attributes', 10, 4 );
 177  
 178  /**
 179   * Create a nav menu item to be displayed on mobile to navigate from submenu back to the parent.
 180   *
 181   * This duplicates each parent nav menu item and makes it the first child of itself.
 182   *
 183   * @param array  $sorted_menu_items Sorted nav menu items.
 184   * @param object $args              Nav menu args.
 185   * @return array Amended nav menu items.
 186   */
 187  function twentynineteen_add_mobile_parent_nav_menu_items( $sorted_menu_items, $args ) {
 188      static $pseudo_id = 0;
 189      if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
 190          return $sorted_menu_items;
 191      }
 192  
 193      $amended_menu_items = array();
 194      foreach ( $sorted_menu_items as $nav_menu_item ) {
 195          $amended_menu_items[] = $nav_menu_item;
 196          if ( in_array( 'menu-item-has-children', $nav_menu_item->classes, true ) ) {
 197              $parent_menu_item                   = clone $nav_menu_item;
 198              $parent_menu_item->original_id      = $nav_menu_item->ID;
 199              $parent_menu_item->ID               = --$pseudo_id;
 200              $parent_menu_item->db_id            = $parent_menu_item->ID;
 201              $parent_menu_item->object_id        = $parent_menu_item->ID;
 202              $parent_menu_item->classes          = array( 'mobile-parent-nav-menu-item' );
 203              $parent_menu_item->menu_item_parent = $nav_menu_item->ID;
 204  
 205              $amended_menu_items[] = $parent_menu_item;
 206          }
 207      }
 208  
 209      return $amended_menu_items;
 210  }
 211  add_filter( 'wp_nav_menu_objects', 'twentynineteen_add_mobile_parent_nav_menu_items', 10, 2 );


Generated : Tue Oct 27 08:20:02 2020 Cross-referenced by PHPXref