template-functions.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. $prefix = '<span class="archive-prefix">' . __( 'Category Archives: ', 'seedlet' ) . '</span>';
  69. $title = '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  70. } elseif ( is_tag() ) {
  71. $prefix = '<span class="archive-prefix">' . __( 'Tag Archives: ', 'seedlet' ) . ' </span>';
  72. $title = '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  73. } elseif ( is_author() ) {
  74. $prefix = '<span class="archive-prefix">' . __( 'Author Archives: ', 'seedlet' ) . ' </span>';
  75. $title = '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>';
  76. } elseif ( is_year() ) {
  77. $prefix = '<span class="archive-prefix">' . __( 'Yearly Archives: ', 'seedlet' ) . ' </span>';
  78. $title = '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'seedlet' ) ) . '</span>';
  79. } elseif ( is_month() ) {
  80. $prefix = '<span class="archive-prefix">' . __( 'Monthly Archives: ', 'seedlet' ) . ' </span>';
  81. $title = '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'seedlet' ) ) . '</span>';
  82. } elseif ( is_day() ) {
  83. $prefix = '<span class="archive-prefix">' . __( 'Daily Archives: ', 'seedlet' ) . ' </span>';
  84. $title = '<span class="page-description">' . get_the_date() . '</span>';
  85. } elseif ( is_post_type_archive() ) {
  86. $prefix = '';
  87. $cpt = get_post_type_object( get_queried_object()->name );
  88. /* translators: %s: Post type singular name */
  89. $title = sprintf(
  90. esc_html__( '%s Archives', 'seedlet' ),
  91. $cpt->labels->singular_name
  92. );
  93. } elseif ( is_tax() ) {
  94. $prefix = '';
  95. $tax = get_taxonomy( get_queried_object()->taxonomy );
  96. /* translators: %s: Taxonomy singular name */
  97. $title = sprintf(
  98. esc_html__( '%s Archives', 'seedlet' ),
  99. $tax->labels->singular_name
  100. );
  101. } else {
  102. $prefix = '';
  103. $title = '<span class="archive-prefix">' . __( 'Archives: ', 'seedlet' ) . ' </span>';
  104. }
  105. return '<h1 class="page-title">' . $prefix . $title . '</h1>';
  106. }
  107. add_filter( 'get_the_archive_title', 'seedlet_get_the_archive_title', 10, 3 );
  108. /**
  109. * Determines if post thumbnail can be displayed.
  110. */
  111. function seedlet_can_show_post_thumbnail() {
  112. return apply_filters( 'seedlet_can_show_post_thumbnail', ! post_password_required() && ! is_attachment() && has_post_thumbnail() );
  113. }
  114. /**
  115. * Returns the size for avatars used in the theme.
  116. */
  117. function seedlet_get_avatar_size() {
  118. return 60;
  119. }
  120. /**
  121. * Returns true if comment is by author of the post.
  122. *
  123. * @see get_comment_class()
  124. */
  125. function seedlet_is_comment_by_post_author( $comment = null ) {
  126. if ( is_object( $comment ) && $comment->user_id > 0 ) {
  127. $user = get_userdata( $comment->user_id );
  128. $post = get_post( $comment->comment_post_ID );
  129. if ( ! empty( $user ) && ! empty( $post ) ) {
  130. return $comment->user_id === $post->post_author;
  131. }
  132. }
  133. return false;
  134. }
  135. /**
  136. * WCAG 2.0 Attributes for Dropdown Menus
  137. *
  138. * Adjustments to menu attributes tot support WCAG 2.0 recommendations
  139. * for flyout and dropdown menus.
  140. *
  141. * @ref https://www.w3.org/WAI/tutorials/menus/flyout/
  142. */
  143. function seedlet_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
  144. // Add [aria-haspopup] and [aria-expanded] to menu items that have children
  145. $item_has_children = in_array( 'menu-item-has-children', $item->classes );
  146. if ( $item_has_children ) {
  147. $atts['aria-haspopup'] = 'true';
  148. $atts['aria-expanded'] = 'false';
  149. }
  150. return $atts;
  151. }
  152. add_filter( 'nav_menu_link_attributes', 'seedlet_nav_menu_link_attributes', 10, 4 );
  153. /*
  154. * Create the continue reading link
  155. */
  156. function seedlet_continue_reading_link() {
  157. if ( ! is_admin() ) {
  158. $continue_reading = sprintf(
  159. /* translators: %s: Name of current post. */
  160. wp_kses( __( 'Continue reading %s', 'seedlet' ), array( 'span' => array( 'class' => array() ) ) ),
  161. the_title( '<span class="screen-reader-text">"', '"</span>', false )
  162. );
  163. return '<a class="more-link" href="' . esc_url( get_permalink() ) . '">' . $continue_reading . '</a>';
  164. }
  165. }
  166. // Filter the excerpt more link
  167. add_filter( 'excerpt_more', 'seedlet_continue_reading_link' );
  168. // Filter the content more link
  169. add_filter( 'the_content_more_link', 'seedlet_continue_reading_link' );
  170. /**
  171. * Add a dropdown icon to top-level menu items.
  172. *
  173. * @param string $output Nav menu item start element.
  174. * @param object $item Nav menu item.
  175. * @param int $depth Depth.
  176. * @param object $args Nav menu args.
  177. * @return string Nav menu item start element.
  178. * Add a dropdown icon to top-level menu items
  179. */
  180. function seedlet_add_dropdown_icons( $output, $item, $depth, $args ) {
  181. // Only add class to 'top level' items on the 'primary' menu.
  182. if ( ! isset( $args->theme_location ) || 'primary' !== $args->theme_location ) {
  183. return $output;
  184. }
  185. if ( 'primary' == $args->theme_location && $depth === 0 ) {
  186. if ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
  187. // Add SVG icon to parent items.
  188. $output .= seedlet_get_icon_svg( 'dropdown', 24 );
  189. }
  190. }
  191. return $output;
  192. }
  193. add_filter( 'walker_nav_menu_start_el', 'seedlet_add_dropdown_icons', 10, 4 );