Filtering Front-End Titles

As part of the ongoing saga of duplicating content, we need a way to see that a particular post is a duplicate and living in a particular module. The easiest way to do that is to add something in the title. Organically, the authors started adding it like Item Title (Module). Now we don’t want that showing up on the front end for users. Now we could just edit that manually when it’s added to the menu but that sucks and it’s another thing to teach people to do and then make sure that they do. That’s bad. Technology can deal with stuff like this and it should.

This is another example of filtering content. The content exists and we just need to chop off the portion in the parenthesis and we need to do it only on the front end.

To make sure it only happens on the front end, we can use is_admin. This is handy function and removing the ! mark would apply the same filter only to the backend.

Then with regular old PHP, we can use strpos to find the first parenthesis and substr to chop the string at that point.

//remove title elements in () on front end
add_filter('the_title', 'ssr_clean_duplicate_title', 10, 2);
function ssr_clean_duplicate_title($title, $id) {
    if ( !is_admin() ) {
        if (strpos($title,"(")>0){
            $title = ssr_remove_parenthesis($title);
        }
    }
    return $title;
}
 
function ssr_remove_parenthesis($title) {
    $position = strpos($title,"(");
    return substr($title, 0, $position);
}