template-functions.php 6.4 KB

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