In my product title, I would like to remove all the text content after the first dot "." or colon ":".
For instance I would like to transform : Theodore Deck : The Peter Marino Collection => Theodore Deck
I tried this code from this articlebut without success.
function explode_parts($title, $id){
$parts = explode(' : ', $title);
$before = $parts[0]; //before the :
$after = $parts[1]; //after the :
return (whatever);
}
add_filter('the_title', 'explode_parts');
Thanks for your help!
In my product title, I would like to remove all the text content after the first dot "." or colon ":".
For instance I would like to transform : Theodore Deck : The Peter Marino Collection => Theodore Deck
I tried this code from this articlebut without success.
function explode_parts($title, $id){
$parts = explode(' : ', $title);
$before = $parts[0]; //before the :
$after = $parts[1]; //after the :
return (whatever);
}
add_filter('the_title', 'explode_parts');
Thanks for your help!
Share Improve this question asked Jun 8, 2020 at 9:27 PardaiglePardaigle 52 bronze badges1 Answer
Reset to default 0Try 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;
}