template-functions.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Functions which enhance the theme by hooking into WordPress
  4. *
  5. * @package Seedlet
  6. * @since 1.0.0
  7. */
  8. /**
  9. * Remove Gutenberg `Theme` Block Styles
  10. */
  11. function seedlet_deregister_styles() {
  12. wp_dequeue_style( 'wp-block-library-theme' );
  13. }
  14. add_action( 'wp_print_styles', 'seedlet_deregister_styles', 100 );
  15. /**
  16. * Adds custom classes to the array of body classes.
  17. *
  18. * @param array $classes Classes for the body element.
  19. * @return array
  20. */
  21. function seedlet_body_classes( $classes ) {
  22. if ( is_singular() ) {
  23. // Adds `singular` to singular pages.
  24. $classes[] = 'singular';
  25. } else {
  26. // Adds `hfeed` to non singular pages.
  27. $classes[] = 'hfeed';
  28. }
  29. // Add a body class if main navigation is active.
  30. if ( has_nav_menu( 'primary' ) ) {
  31. $classes[] = 'has-main-navigation';
  32. }
  33. return $classes;
  34. }
  35. add_filter( 'body_class', 'seedlet_body_classes' );
  36. /**
  37. * Adds custom class to the array of posts classes.
  38. */
  39. function seedlet_post_classes( $classes, $class, $post_id ) {
  40. $classes[] = 'entry';
  41. return $classes;
  42. }
  43. add_filter( 'post_class', 'seedlet_post_classes', 10, 3 );
  44. /**
  45. * Add a pingback url auto-discovery header for single posts, pages, or attachments.
  46. */
  47. function seedlet_pingback_header() {
  48. if ( is_singular() && pings_open() ) {
  49. echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  50. }
  51. }
  52. add_action( 'wp_head', 'seedlet_pingback_header' );
  53. /**
  54. * Changes comment form default fields.
  55. */
  56. function seedlet_comment_form_defaults( $defaults ) {
  57. $comment_field = $defaults['comment_field'];
  58. // Adjust height of comment form.
  59. $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $comment_field );
  60. return $defaults;
  61. }
  62. add_filter( 'comment_form_defaults', 'seedlet_comment_form_defaults' );
  63. /**
  64. * Filters the default archive titles.
  65. */
  66. function seedlet_get_the_archive_title() {
  67. if ( is_category() ) {
  68. $title = __( 'Category Archives: ', 'seedlet' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  69. } elseif ( is_tag() ) {
  70. $title = __( 'Tag Archives: ', 'seedlet' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  71. } elseif ( is_author() ) {
  72. $title = __( 'Author Archives: ', 'seedlet' ) . '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>';
  73. } elseif ( is_year() ) {
  74. $title = __( 'Yearly Archives: ', 'seedlet' ) . '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'seedlet' ) ) . '</span>';
  75. } elseif ( is_month() ) {
  76. $title = __( 'Monthly Archives: ', 'seedlet' ) . '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'seedlet' ) ) . '</span>';
  77. } elseif ( is_day() ) {
  78. $title = __( 'Daily Archives: ', 'seedlet' ) . '<span class="page-description">' . get_the_date() . '</span>';
  79. } elseif ( is_post_type_archive() ) {
  80. $cpt = get_post_type_object( get_queried_object()->name );
  81. /* translators: %s: Post type singular name */
  82. $title = sprintf( esc_html__( '%s Archives', 'seedlet' ),
  83. $cpt->labels->singular_name
  84. );
  85. } elseif ( is_tax() ) {
  86. $tax = get_taxonomy( get_queried_object()->taxonomy );
  87. /* translators: %s: Taxonomy singular name */
  88. $title = sprintf( esc_html__( '%s Archives', 'seedlet' ),
  89. $tax->labels->singular_name
  90. );
  91. } else {
  92. $title = __( 'Archives:', 'seedlet' );
  93. }
  94. return $title;
  95. }
  96. add_filter( 'get_the_archive_title', 'seedlet_get_the_archive_title' );
  97. /**
  98. * Determines if post thumbnail can be displayed.
  99. */
  100. function seedlet_can_show_post_thumbnail() {
  101. return apply_filters( 'seedlet_can_show_post_thumbnail', ! post_password_required() && ! is_attachment() && has_post_thumbnail() );
  102. }
  103. /**
  104. * Returns the size for avatars used in the theme.
  105. */
  106. function seedlet_get_avatar_size() {
  107. return 60;
  108. }
  109. /**
  110. * Returns true if comment is by author of the post.
  111. *
  112. * @see get_comment_class()
  113. */
  114. function seedlet_is_comment_by_post_author( $comment = null ) {
  115. if ( is_object( $comment ) && $comment->user_id > 0 ) {
  116. $user = get_userdata( $comment->user_id );
  117. $post = get_post( $comment->comment_post_ID );
  118. if ( ! empty( $user ) && ! empty( $post ) ) {
  119. return $comment->user_id === $post->post_author;
  120. }
  121. }
  122. return false;
  123. }
  124. /**
  125. * WCAG 2.0 Attributes for Dropdown Menus
  126. *
  127. * Adjustments to menu attributes tot support WCAG 2.0 recommendations
  128. * for flyout and dropdown menus.
  129. *
  130. * @ref https://www.w3.org/WAI/tutorials/menus/flyout/
  131. */
  132. function seedlet_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
  133. // Add [aria-haspopup] and [aria-expanded] to menu items that have children
  134. $item_has_children = in_array( 'menu-item-has-children', $item->classes );
  135. if ( $item_has_children ) {
  136. $atts['aria-haspopup'] = 'true';
  137. $atts['aria-expanded'] = 'false';
  138. }
  139. return $atts;
  140. }
  141. add_filter( 'nav_menu_link_attributes', 'seedlet_nav_menu_link_attributes', 10, 4 );
  142. /*
  143. * Create the continue reading link
  144. */
  145. function seedlet_continue_reading_link() {
  146. if ( ! is_admin() ) {
  147. $continue_reading = sprintf(
  148. /* translators: %s: Name of current post. */
  149. wp_kses( __( 'Continue reading %s', 'seedlet' ), array( 'span' => array( 'class' => array() ) ) ),
  150. the_title( '<span class="screen-reader-text">"', '"</span>', false )
  151. );
  152. return '<a class="more-link" href="' . esc_url( get_permalink() ) . '">' . $continue_reading . '</a>';
  153. }
  154. }
  155. // Filter the excerpt more link
  156. add_filter( 'excerpt_more', 'seedlet_continue_reading_link' );
  157. // Filter the content more link
  158. add_filter( 'the_content_more_link', 'seedlet_continue_reading_link' );
  159. /**
  160. * Add a dropdown icon to top-level menu items.
  161. *
  162. * @param string $output Nav menu item start element.
  163. * @param object $item Nav menu item.
  164. * @param int $depth Depth.
  165. * @param object $args Nav menu args.
  166. * @return string Nav menu item start element.
  167. * Add a dropdown icon to top-level menu items
  168. */
  169. function seedlet_add_dropdown_icons( $output, $item, $depth, $args ) {
  170. // Only add class to 'top level' items on the 'primary' menu.
  171. if ( ! isset( $args->theme_location ) || 'primary' !== $args->theme_location ) {
  172. return $output;
  173. }
  174. if ( 'primary' == $args->theme_location && $depth === 0 ) {
  175. if ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
  176. // Add SVG icon to parent items.
  177. $output .= seedlet_get_icon_svg( 'dropdown', 24 );
  178. }
  179. }
  180. return $output;
  181. }
  182. add_filter( 'walker_nav_menu_start_el', 'seedlet_add_dropdown_icons', 10, 4 );