We love K2 – it’s one of those rare components that are a true a gem. Nevertheless, we do discover some quirks in the K2 engine from time to time. Today, we have discovered a problem with setting the Open Graph description meta tag. Here’s what happened this afternoon…
In the HTML code of one of our clients, we saw the following meta tag:
<meta name="og:description" content="Tweet Share [Real Description of the Page...]" />
We didn’t know where Tweet and Share were coming from, they were nowhere to be found anywhere in the template. Additionally, we know for a fact that Joomla – by default – doesn’t set the og:description meta tag. It must be a plugin, but which? We immediately thought about sh404 (it’s, ahem, favorite suspect), so we checked all its code and we discovered that it was innocent. Yes, there is a place where sh404 sets the og:description meta tag, but a quick test revealed that it wasn’t that code that was generating og:description. The Open Graph description meta tag was generated and set somewhere else…
We spent a long time trying to search for the plugin that was doing it and we couldn’t find any. But then we thought, what if that code is generated by K2? It does make sense because K2 is aware of its content and is known for having native support for Open Graph. A quick search in K2’s core files unveiled the mystery – it was the file view.html.php (located under the components/com_k2/views/item folder) that was setting the og:description meta tag. OK – now you might be thinking – what does that have anything to do with the presence of Tweet and Share in the og:description meta tag. Well, the problem was that K2 was grabbing the og:description (as well as the standard meta description) from the document, and not from the K2 item content. This is fine, but when the document’s content is changed by a plugin (such as a plugin that inserts a Tweet and a Share button), then the og:description will be polluted by nonsense words such as Tweet, Share, and Script…
OK – enough talk! Here’s what we did to solve the problem:
- We opened the file view.html.php which exists under the folder components/com_k2/views/item.
-
We changed the following line (line 473):
$document->setMetaData('og:description', htmlspecialchars(strip_tags($document->getDescription()), ENT_QUOTES, 'UTF-8'));
to the following line:
$document->setMetaData('og:description', htmlspecialchars(strip_tags($introtext), ENT_QUOTES, 'UTF-8'));
-
We added the following line:
$introtext = trim($item->introtext);
After this line (line 42):
$item = $model->getData();
-
We uploaded the view.html.php back to its place and that solved the problem!
Note that also the standard description meta tag might also be wrong if you’re not using K2’s meta description field. In this case, you will need to change this line (line 386):
$metaDescItem = preg_replace("#{(.*?)}(.*?){/(.*?)}#s", '', $item->introtext.' '.$item->fulltext);
to this one:
$metaDescItem = preg_replace("#{(.*?)}(.*?){/(.*?)}#s", '', $introtext.' '.$item->fulltext);
So, yes, even K2 can have bugs. Even Joomla can have bugs, for that matter! But that doesn’t mean that they’re not both excellent products!
Now, if your (og:)description meta tag has some weird content, then please apply the above and see if it works. If it doesn’t, or if you don’t even have K2 in the first place, then the problem might be caused by another extension. In this case, just contact us and we’ll help you solve the problem quickly. By the way, fear not, we’ll charge you a very reasonable fee for our services!
[…] love K2 (we actually said that before) and we think that it’s a better content manager than Joomla’s own content manager. In […]
Tried the proposed solution on my website. Did not work – still wrong title in Facebook. On the other hand it didn’t change anything either.
Hi Thorbjorn,
Are you sure that:
– You have cleared your Joomla cache.
– The page(s) in question is(are) using K2.
– You are working on the right file location?
– You have checked the code to to see that nothing has changed? (It might be that the code has changed and the og meta description has changed, but Facebook is still caching and old copy of your page(s))
The reason why I’m asking these questions is because we have used this solution flawlessly on many websites so far.