extras.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Custom functions that act independently of the theme templates.
  4. *
  5. * Eventually, some of the functionality here could be replaced by core features.
  6. *
  7. * @package Rebalance
  8. */
  9. /**
  10. * Adds custom classes to the array of body classes.
  11. *
  12. * @param array $classes Classes for the body element.
  13. * @return array
  14. */
  15. function rebalance_body_classes( $classes ) {
  16. // Adds a class of group-blog to blogs with more than 1 published author.
  17. if ( is_multi_author() ) {
  18. $classes[] = 'group-blog';
  19. }
  20. // Adds a class of hfeed to non-singular pages.
  21. if ( ! is_singular() ) {
  22. $classes[] = 'hfeed';
  23. }
  24. // Adds a class of hfeed to non-singular pages.
  25. $header_image = get_header_image();
  26. if ( ! empty( $header_image ) ) {
  27. $classes[] = 'has-custom-header';
  28. }
  29. return $classes;
  30. }
  31. add_filter( 'body_class', 'rebalance_body_classes' );
  32. /**
  33. * Shorten Excerpt Length
  34. */
  35. function rebalance_excerpt_length() {
  36. return 20;
  37. }
  38. add_filter( 'excerpt_length', 'rebalance_excerpt_length', 999 );
  39. /**
  40. * Add ellipses to excerpts
  41. */
  42. function rebalance_excerpt_more() {
  43. if ( is_home() ) {
  44. return '&hellip;';
  45. } else {
  46. return ' &hellip; <a class="more-link" href="' . esc_url( get_permalink() ) . '">' . esc_html__( 'More', 'rebalance' ) . '</a>';
  47. }
  48. }
  49. add_filter( 'excerpt_more', 'rebalance_excerpt_more' );
  50. /**
  51. * Add conditional classes to posts
  52. */
  53. function rebalance_slug_post_classes( $classes ) {
  54. /**
  55. * Adds clear fix to single posts
  56. */
  57. if ( is_single() ) {
  58. $classes[] = 'clear-fix';
  59. }
  60. /**
  61. * Use card display on archive and search pages
  62. */
  63. if ( is_archive() || is_search() ) {
  64. $classes[] = 'card';
  65. }
  66. /*
  67. * '.card' class not needed when using default page templates on front page
  68. * - default page template conditional src: https://goo.gl/QOMYWP
  69. */
  70. if ( ( is_page() && ! is_page_template() ) && is_front_page() ) {
  71. $classes[] = '';
  72. } elseif ( is_page_template( 'portfolio-page.php' ) ) {
  73. $classes[] = 'card';
  74. } elseif ( ( is_home() && ! is_page_template() ) || ( is_front_page() && ! is_page_template() ) ) {
  75. $classes[] = 'card';
  76. } else {
  77. $classes[] = '';
  78. }
  79. return $classes;
  80. }
  81. add_filter( 'post_class', 'rebalance_slug_post_classes', 10, 3 );
  82. add_filter( 'comment_form_default_fields', 'rebalance_comment_placeholders' );
  83. /**
  84. * Change default fields, add placeholder and change type attributes.
  85. *
  86. * @param array $fields
  87. * @return array
  88. */
  89. function rebalance_comment_placeholders( $fields ) {
  90. $fields['author'] = str_replace(
  91. '<input',
  92. '<input placeholder="'
  93. /* Replace 'rebalance' with your theme?s text domain.
  94. * I use _x() here to make your translators life easier. :)
  95. * See http://codex.wordpress.org/Function_Reference/_x
  96. */
  97. . esc_attr_x(
  98. 'ex: jane doe',
  99. 'comment form placeholder',
  100. 'rebalance'
  101. )
  102. . '"',
  103. $fields['author']
  104. );
  105. $fields['email'] = str_replace(
  106. '<input',
  107. /* We use a proper type attribute to make use of the browser's
  108. * validation, and to get the matching keyboard on smartphones.
  109. */
  110. '<input type="email" placeholder="'. esc_attr_x( 'ex: janedoe@gmail.com', 'Email input placeholder text', 'rebalance' ) .'"',
  111. $fields['email']
  112. );
  113. $fields['url'] = str_replace(
  114. '<input',
  115. // Again: a better 'type' attribute value.
  116. '<input placeholder="' . esc_attr_x( 'ex: http://janedoe.wordpress.com', 'URL input placeholder text', 'rebalance' ) . '"',
  117. $fields['url']
  118. );
  119. return $fields;
  120. }