Keyword

Add FAcebook image meta tag to K2 head (og:image)

More
10 years 11 months ago #119960 by Vseva
Hey;

Thought this might be useful, even though it is a core hack; and I'm sure it could be improved too.

I noticed K2 doesn't insert the "og:image" mete tag into the head of pages.
<meta property="og:image" content="http://example.com/images/logo.png" />

So, if you go to: /components/com_k2/views/itemlist/view.html.php at line 637
Where you find this conditional statement:
if ($task == 'category' && $this->category->image && strpos($this->category->image, 'placeholder/category.png') === false)
                {
                        $image = substr(JURI::root(), 0, -1).str_replace(JURI::root(true), '', $this->category->image);
                        $document->setMetaData('og:image', $image);
                        $document->setMetaData('image', $image);
                }

and add an else statement right after the " } ", and paste this code in:
else{
        // New code
            $image = substr(JURI::root(), 0, -1).str_replace(JURI::root(true), '', '/images/logo.png');
            $document->setMetaData('og:image', $image);
                        $document->setMetaData('image', $image);

        }

        if ($task == 'item' && !empty($this->item->image)){
            $image = substr(JURI::root(), 0, -1).str_replace(JURI::root(true), '', $this->item->image);
                        $document->setMetaData('og:image', $image);
                        $document->setMetaData('image', $image);

        }else{
            $image = substr(JURI::root(), 0, -1).str_replace(JURI::root(true), '', '/images/logo.png');
            $document->setMetaData('og:image', $image);
                        $document->setMetaData('image', $image);

        }
        // End of New code

Code explained:

If there is a category image assigned to the category it will add it to the meta tag, if not(else) it will use the logo.png in the images folder. (you might want to substitute (logo.png) for whatever image you want to use by default).
And same goes for an item.

I know it's a hack, but it did the trick for me.

Hope someone finds this useful.

Peace.

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


Powered by Kunena Forum