Filter and clean the Archive Title

This client wanted to feature their archive titles in a large hero area. The PSD excluded the prefixes like, “Category:”, “Tag:”, “Author:”, etc…

The snippet below allows you to strip out the prefixes used in the get_the_archive_title(); function, as well as any HTML tags.

Screenshot of the clean archive title
Screenshot of the “clean” archive title.

Jeremy Pry helped me solve this by inserting this regular expression into PHP’s preg_replace();  function. It executes a find and replace on the $title variable.

 * Remove archive title prefixes.
 * @param  string  $title  The archive title from get_the_archive_title();
 * @return string          The cleaned title.
function grd_custom_archive_title( $title ) {
	// Remove any HTML, words, digits, and spaces before the title.
	return preg_replace( '#^[\w\d\s]+:\s*#', '', strip_tags( $title ) );
add_filter( 'get_the_archive_title', 'grd_custom_archive_title' );

By Greg Rickaby

Director of Engineering @WebDevStudios / Author & Editor @Wiley

One reply on “Filter and clean the Archive Title”

Leave a Reply