Guest User

Untitled

a guest
Dec 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. <?php
  2. add_filter( 'the_title', 'my_capitalize_title', 10, 2 );
  3.  
  4. function my_capitalize_title( $title, $id ) {
  5.  
  6. // get separate words
  7. $words = preg_split( '/[^w]*([s]+[^w]*|$)/', $title, NULL, PREG_SPLIT_NO_EMPTY );
  8.  
  9. $stop_words = array(
  10. 'the', //
  11. 'a',
  12. 'and',
  13. 'of',
  14. );
  15.  
  16. $title_case = '';
  17.  
  18. foreach( $words as $word ) {
  19. // concatenate stop word intact
  20. if ( in_array( $word, $stop_words ) ) {
  21. $title_case .= $word;
  22. }
  23. // or concatenate capitalized word
  24. $title_case .= ucfirst( $word );
  25. }
  26.  
  27. return $title_case;
  28. }
Add Comment
Please, Sign In to add comment