template-functions.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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( $title, $original_title, $prefix ) {
  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. $prefix = '<span class="archive-prefix">' . $prefix . '</span>';
  96. $title = '<span class="archive-title">' . $original_title . '</span>';
  97. return '<h1 class="page-title">' . $prefix . $title . '</h1>';
  98. }
  99. add_filter( 'get_the_archive_title', 'seedlet_get_the_archive_title', 10, 3 );
  100. /**
  101. * Determines if post thumbnail can be displayed.
  102. */
  103. function seedlet_can_show_post_thumbnail() {
  104. return apply_filters( 'seedlet_can_show_post_thumbnail', ! post_password_required() && ! is_attachment() && has_post_thumbnail() );
  105. }
  106. /**
  107. * Returns the size for avatars used in the theme.
  108. */
  109. function seedlet_get_avatar_size() {
  110. return 60;
  111. }
  112. /**
  113. * Returns true if comment is by author of the post.
  114. *
  115. * @see get_comment_class()
  116. */
  117. function seedlet_is_comment_by_post_author( $comment = null ) {
  118. if ( is_object( $comment ) && $comment->user_id > 0 ) {
  119. $user = get_userdata( $comment->user_id );
  120. $post = get_post( $comment->comment_post_ID );
  121. if ( ! empty( $user ) && ! empty( $post ) ) {
  122. return $comment->user_id === $post->post_author;
  123. }
  124. }
  125. return false;
  126. }
  127. /**
  128. * WCAG 2.0 Attributes for Dropdown Menus
  129. *
  130. * Adjustments to menu attributes tot support WCAG 2.0 recommendations
  131. * for flyout and dropdown menus.
  132. *
  133. * @ref https://www.w3.org/WAI/tutorials/menus/flyout/
  134. */
  135. function seedlet_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
  136. // Add [aria-haspopup] and [aria-expanded] to menu items that have children
  137. $item_has_children = in_array( 'menu-item-has-children', $item->classes );
  138. if ( $item_has_children ) {
  139. $atts['aria-haspopup'] = 'true';
  140. $atts['aria-expanded'] = 'false';
  141. }
  142. return $atts;
  143. }
  144. add_filter( 'nav_menu_link_attributes', 'seedlet_nav_menu_link_attributes', 10, 4 );
  145. /*
  146. * Create the continue reading link
  147. */
  148. function seedlet_continue_reading_link() {
  149. if ( ! is_admin() ) {
  150. $continue_reading = sprintf(
  151. /* translators: %s: Name of current post. */
  152. wp_kses( __( 'Continue reading %s', 'seedlet' ), array( 'span' => array( 'class' => array() ) ) ),
  153. the_title( '<span class="screen-reader-text">"', '"</span>', false )
  154. );
  155. return '<a class="more-link" href="' . esc_url( get_permalink() ) . '">' . $continue_reading . '</a>';
  156. }
  157. }
  158. // Filter the excerpt more link
  159. add_filter( 'excerpt_more', 'seedlet_continue_reading_link' );
  160. // Filter the content more link
  161. add_filter( 'the_content_more_link', 'seedlet_continue_reading_link' );
  162. /**
  163. * Add a dropdown icon to top-level menu items.
  164. *
  165. * @param string $output Nav menu item start element.
  166. * @param object $item Nav menu item.
  167. * @param int $depth Depth.
  168. * @param object $args Nav menu args.
  169. * @return string Nav menu item start element.
  170. * Add a dropdown icon to top-level menu items
  171. */
  172. function seedlet_add_dropdown_icons( $output, $item, $depth, $args ) {
  173. // Only add class to 'top level' items on the 'primary' menu.
  174. if ( ! isset( $args->theme_location ) || 'primary' !== $args->theme_location ) {
  175. return $output;
  176. }
  177. if ( 'primary' == $args->theme_location && $depth === 0 ) {
  178. if ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
  179. // Add SVG icon to parent items.
  180. $output .= seedlet_get_icon_svg( 'dropdown', 24 );
  181. }
  182. }
  183. return $output;
  184. }
  185. add_filter( 'walker_nav_menu_start_el', 'seedlet_add_dropdown_icons', 10, 4 );