Limit the meta description by word count
- Thread started
- Last updated
What you’ll achieve
Limit the page’s meta description to a chosen number of words without changing visible or saved descriptions.
My site
Limit the words in the page's description metatag without shortening the saved or visible listing description. This metatag is a summary in the page source that search engines can use in search results; it is separate from the description visitors read on the listing.
Choose the word limit and pages
The examples truncate description metatags to 20 words. They do not alter the visible description or its stored value. These filters are not restricted to one listing type. Check every page that uses the selected description filter.
Start a new PHP file with <?php followed by defined('MVC_FRAMEWORK') or die;. If the file already exists, add the example without repeating those lines.
JReviews 6 Early Access
Add the filter
Joomla. Use templates/jreviews_overrides/hooks.php.
WordPress. Use jreviews_overrides/hooks.php.
Requires Listing Detail Page in Early Access.
Add the callback marked with +. It shortens the description metatag.
- If
hooks.phpalready exists, add the marked lines at the end of the file. The copy button copies only those lines. - If you are creating
hooks.php, type the two dimmed lines first, then add the marked lines below them.
<?php
defined('MVC_FRAMEWORK') or die;
fwd_add_filter('jreviews:page_meta:description', function ($value, $name, $pageType, $attributes) {
$words = preg_split('/\s+/u', trim(strip_tags((string) $value)), -1, PREG_SPLIT_NO_EMPTY);
if ($words === false) {
return $value;
}
return implode(' ', array_slice($words, 0, 20));
}, 20, 4);
The callback removes HTML, counts words separated by whitespace, and keeps the first 20 words without adding an ellipsis. It applies wherever JReviews uses this Early Access description filter, so check other affected pages too.
JReviews 5 and v6 Legacy
Add the filter
Joomla. Use templates/jreviews_overrides/filters/filter_functions.php.
WordPress. Use jreviews_overrides/filters/filter_functions.php.
Add the function and filter marked with +. The legacy hook uses the text helper's word truncation.
- If
filter_functions.phpalready exists, add the marked lines at the end of the file. The copy button copies only those lines. - If you are creating
filter_functions.php, type the two dimmed lines first, then add the marked lines below them.
<?php
defined('MVC_FRAMEWORK') or die;
function change_description_metatag($description, $params)
{
S2App::import('Helper','text','jreviews');
$TextHelper = new TextHelper;
return $TextHelper->truncateWords($description,20);
}
Clickfwd\Hook\Filter::add('page_description_metatag', 'change_description_metatag', 10);
Change 20 in truncateWords() to the desired limit. Test each page where the filter applies.
Check the description metatag in the page source
Open pages with a long description, a short description, and an empty description. On each page, right-click and choose View page source, then search for <meta name="description". Confirm that its content value has the chosen word limit. Check that the visible description and the text in the listing edit form remain intact. If you find conflicting description tags, check any SEO extension that also adds them.