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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref