Try using following code:
add_filter('the_title', 'mod_product__title', 10, 2);
function mod_product__title($title, $id) {
if( is_product() ) {
if(preg_match('/[^(:|.)]*/', $title, $matches)){
return trim($matches[0]);
}else{
return $title;
}
}
return $title;
}
Here I’m using regex to match .
or :
. Regex will match only the first occurrence and return the text before it. Then I’m using trim()
to get rid of any extra trailing space. If it doesn’t match anything then it will return the full title.
UPDATE
Following code checks up wheather it’s admin list or front-end and modifies title accordingly
add_filter('the_title', 'mod_product__title', 10, 2);
function mod_product__title($title, $id) {
global $pagenow;
if ( $pagenow != 'edit.php' && get_post_type( $id ) == 'product' ) {
if(preg_match('/[^(:|.)]*/', $title, $matches)){
return trim($matches[0]);
}else{
return $title;
}
}
return $title;
}