Skip to content

Instantly share code, notes, and snippets.

@ericpedia
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericpedia/6c6f4bda33d5a7e0f2c2 to your computer and use it in GitHub Desktop.
Save ericpedia/6c6f4bda33d5a7e0f2c2 to your computer and use it in GitHub Desktop.
WordPress: Remove date ranges from post titles
<?php
/**
* Remove date ranges from post titles
*
* @author ericpedia
* @since 2014-08-26
*
*/
add_filter( 'the_title', 'epi_remove_dates_from_charts' );
function epi_remove_dates_from_charts( $title ) {
global $post;
// $isChart = is_singular( array('chart', 'post') ); // no idea why this didn't work
$isChart = in_array( $post->post_type, array('chart', 'post') );
if ( ! $isChart ) return $title;
$months = '((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(t|tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\.?)';
// @todo doesn't catch the years at the end of: "Change in real family income from business cycle peak years 1989, 2000, and"
$filtered = preg_replace( '/,? ('.$months.'?[\s-–—]?\d+([-–— and]+'.$months.'?[\s-–—]?\d+)?( \(.*dollars.*\))?\*?$)/', '', $title );
// To privately test you can uncomment this
// if ( is_user_logged_in() ) {
// return "$title<br>$filtered";
// // return "$title<br>$filtered";
// }
return $filtered;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment