template-functions.php 7.4 KB

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