read an article on Google that says for good SEO it is best that the size of the slug in the URL is limited to 5 words.
As I use WordPress the link is assigned automatically to the article title. To redo all the links with just 5 words I would have to spend months editing all the links on my blog.
Would it be possible to do this automatically? There is some function or code for that. I found this code and added it to the function page of my theme and had no results.
See the code:
function pm_limit_slugs_length($uri) {
$max_words = 5; // If any part of URI contains more than 5 words, the slug will be limited to first 5 words
$new_title = '';
$slugs = explode('/', $uri);
for($i=0, $count = count($slugs); $i < $count; $i++) {
$slug = $slugs[$i];
$words = explode('-', $slug);
$new_title .= "/";
if(count($words) > $max_words) {
$new_title .= implode("-", array_slice($words, 0, $max_words));
} else {
$new_title .= $slug;
}
}
// Remove trailing slashes
$new_title = trim($new_title, "/");
return $new_title;
}
add_filter('permalink_manager_filter_default_post_uri', 'pm_limit_slugs_length', 99);
add_filter('permalink_manager_filter_default_term_uri', 'pm_limit_slugs_length', 99);
I found the code here: https://permalinkmanager.pro/docs/filters-hooks/how-to-limit-the-number-of-words-in-wordpress-permalinks/
How can I use it to limit the Wordpress slug size to 5 words?