Marc Blase

Match and replace content in WordPress the_title()

In a situation where content in the_title() needed to be italicized.

function fix_the_title( $title ) {

  $new_title = $title;
  $pattern = '/Content to Match/';
  $replace = '<em>$0</em>';
  $new_title = preg_replace($pattern, $replace, $new_title);

  return $new_title;
}
add_filter( 'the_title', 'fix_the_title' );

Add that to functions.php and you’re good.

Published on July 1, 2020