Shorten Joomla 3 RSS feed description.

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active

Joomla does rss feeds, that's good.  The problem is the rss feed descriptions include the entire contents of the articles included in the feed and this just seems really dumb to me.  Why?  Because the feeds usually give a short snippet of the page they point to along with a URL to the page.  If you want to read the entire page you would actually click the link and go load the web page.  

I don't know why it was done this way, I really don't care.  One of the great things about Joomla is that is uses overrides so that you can modify it without changing the core code. This works very well because your custom code doesn't get clobbered every time there's an update.  There's a problem when it comes to rss feeds in Joomla though, the code that generates it's rss feeds is in the libraries directory which, according to my googling, cannot be overridden like other code can. 

I did find some confusing information about using a classloader and some additional customizations that should work but quite frankly, it sounded like a bit more work than I was willing to do.  I decided to go ahead and modify the core code knowing full well that I will potentially have to fix it (re-upload two files) every time I do an update.  I can live with that.

The file I modified was /public_html/libraries/src/Document/Renderer/Feed/RssRenderer.php.   There's a few places in the code that contained <description> tags.  I modified the code so that each description would be stripped of html tags and truncated to 50 words using implode and explode (see this web page), for example:

// original line
//$feed .= " <description><![CDATA[" . $this->_relToAbs($data->items[$i]->description) . "]]></description>\n";

// New code 
$pieces = explode(" ", strip_tags($this->_relToAbs($data->items[$i]->description)));

$newdesc = implode(" ", array_splice($pieces, 0, 50));
$feed .= " <description><![CDATA[" . $newdesc . "... ]]></description>\n";

I then added an ellipsis to the closing CDATA tag (see above) to indicate there was more text being omitted.  The resulting rss can bee seen in this snippit:

<title>Delta Chamberlain 8 in. Widespread 2-Handle Bathroom Faucet in SpotShield Brushed Nickel Review</title>
<guid isPermaLink="true"></guid>
<description><![CDATA[I used this faucet to replace my existing older American Standard widespread faucet. One of the nice features of it is Delta's Spotshield technology. After using it for a while I have to say this faucet is far more resistant to spotting than my old one. Even if you get.... ]]></description>
<author>This email address is being protected from spambots. You need JavaScript enabled to view it. (Administrator)</author>
<pubDate>Wed, 19 Feb 2020 17:36:57 +0000</pubDate>

You can see the result is 50 words, plus the ellipsis at the end. Hope you find this helpful, if you want the code, sent me a note on my contact form on the contact page.



* indicates required

Powered by MailChimp