template-functions.php 7.3 KB

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