4
21/10/2024 10:53 e m
Ämnesstart
I am using WPForo as my WordPress forum plugin of choice, and I am pretty happy with it. 🙂
My question is: How can I change the SEO meta title to trim it to 65 characters? Perhaps a custom function in the child theme or modifying some code in a hook?
Tack så mycket!
1 Svar
3
21/10/2024 11:03 e m
In your Child Theme - in the functions.php put this code (pls read the comments):
function max_title_length( $meta_title ) {
$max = 65;
// Convert array elements into a string, or cast input to string
$title = is_array($meta_title) ? implode(' – ', array_filter($meta_title)) : (string) $meta_title;
// Check and trim the title if it exceeds the max length
if (strlen($title) > $max) {
// Limit the length and find the last space within the allowed limit
$title = substr($title, 0, $max);
$last_space_position = strrpos($title, ' ');
// Trim to the last full word if possible
if ($last_space_position !== false) {
$title = substr($title, 0, $last_space_position);
}
// Remove any trailing separator and add " …"
$title = rtrim($title, ' –') . " …";
}
// Return an array if the input was an array, otherwise return a string
return is_array($meta_title) ? explode(' – ', $title) : $title;
}
add_filter('wpforo_seo_topic_title', 'max_title_length');
The function simplifies the trimming and separator handling to deliver clean results efficiently, while keeping the code easy to read and flexible.
