Keyword

Meta and OG description improvement

  • Ruud van Lent
  • Ruud van Lent's Avatar Topic Author
  • Offline
  • New Member
More
2 years 7 months ago - 2 years 7 months ago #153683 by Ruud van Lent
Ruud van Lent created the topic: Meta and OG description improvement
Hi,

first post here :)

I developed a Joomla! plugin that automatically adds Social Sharing buttons to top / bottom of articles. In last version I have added support for K2.

What the plugin does is add a script to the specified position of the article (or Item when in K2).
the script looks like this:
<script type="javascript/text>herecomesthejquerycode</script>

this works (almost) perfect.
Where this goes wrong is when the article has no meta description and K2 generates one from the intro- or full-text.
K2 uses the following code for this in file ./component/com_k2/views/item/view.html.php
                        $metaDescItem = preg_replace("#{(.*?)}(.*?){/(.*?)}#s", '', $item->introtext.' '.$item->fulltext);
                        $metaDescItem = strip_tags($metaDescItem);
                        $metaDescItem = K2HelperUtilities::characterLimit($metaDescItem, $params->get('metaDescLimit', 150));
                        $document->setDescription(K2_JVERSION == '15' ? $metaDescItem : $metaDescItem);

The 'cullprit' is in the strip_tags function.
This strips the tags from the description but does not strip the code itself!
so <script type="javascript/text>herecomesthejquerycode</script> becomes herecomesthejquerycode

When posting that page on Facebook (and G+) the text that is being posted starts with "herecomesthejquerycode'... which is wrong :(

I have solved this with a simple regex. The new code in view.html.php will look like (line 2 is added):
                        $metaDescItem = preg_replace("#{(.*?)}(.*?){/(.*?)}#s", '', $item->introtext.' '.$item->fulltext);
                        $metaDescItem = preg_replace("/<script\b[^>]*>(.*?)<\/script>/is", '', $item->introtext.' '.$item->fulltext);
                        $metaDescItem = strip_tags($metaDescItem);
                        $metaDescItem = K2HelperUtilities::characterLimit($metaDescItem, $params->get('metaDescLimit', 150));
                        $document->setDescription(K2_JVERSION == '15' ? $metaDescItem : $metaDescItem);

What this code (second preg_replace) does is strip the <script...> and </script> tags AND everything (the script itself) in between :)

Hope this helps!
regards,
Ruud.

Sharing = Caring
Last Edit: 2 years 7 months ago by Ruud van Lent. Reason: typo

Please Log in or Create an account to join the conversation.

More
2 years 7 months ago #153710 by Krikor Boghossian
Krikor Boghossian replied the topic: Meta and OG description improvement
Hello Ruud,

Kudos on your plugin :) and thank you so much for posting this.
Can you make a PR on Github so we can evaluate and merge it?
github.com/getk2/k2

IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

---
JoomlaWorks
www.joomlaworks.net/

Please Log in or Create an account to join the conversation.

  • Ruud van Lent
  • Ruud van Lent's Avatar Topic Author
  • Offline
  • New Member
More
2 years 7 months ago #153721 by Ruud van Lent
Ruud van Lent replied the topic: Meta and OG description improvement

Krikor wrote: Hello Ruud,

Kudos on your plugin :) and thank you so much for posting this.
Can you make a PR on Github so we can evaluate and merge it?
github.com/getk2/k2


Hi, here you go: github.com/getk2/k2/pull/311

and thanks for the kudos :)

Sharing = Caring

Please Log in or Create an account to join the conversation.

More
2 years 7 months ago #153722 by Krikor Boghossian
Krikor Boghossian replied the topic: Meta and OG description improvement
Thanks mate :)
You 'll be notified via Github.

IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

---
JoomlaWorks
www.joomlaworks.net/

Please Log in or Create an account to join the conversation.

Moderators: william white