template-functions.php 6.9 KB

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