Keyword

SOLVED WITH HACK - Using Extra Field Image for og:image

  • popper
  • popper's Avatar Topic Author
  • Offline
  • New Member
More
7 years 10 months ago - 7 years 10 months ago #163092 by popper
In file www\components\com_k2\views\item\view.html.php I replaced
// Set Facebook meta data
		if($params->get('facebookMetatags', '1'))
		{
			$document = JFactory::getDocument();
			$uri = JURI::getInstance();
			$document->setMetaData('og:url', $uri->toString());
			$document->setMetaData('og:title', (K2_JVERSION == '15') ? htmlspecialchars($document->getTitle(), ENT_QUOTES, 'UTF-8') : $document->getTitle());
			$document->setMetaData('og:type', 'article');
			$facebookImage = 'image'.$params->get('facebookImage', 'Small');
			if ($item->$facebookImage)
			{
				$basename = basename($item->$facebookImage);
				if(strpos($basename, '?t=')!==false)
				{
					$tmpBasename = explode('?t=', $basename);
					$basenameWithNoTimestamp = $tmpBasename[0];
				}
				else
				{
					$basenameWithNoTimestamp = $basename;
				}
				if (JFile::exists(JPATH_SITE.'/media/k2/items/cache/'.$basenameWithNoTimestamp))
				{
					$image = JURI::root().'media/k2/items/cache/'.$basename;
					$document->setMetaData('og:image', $image);
					$document->setMetaData('image', $image);
				}
			}
			$document->setMetaData('og:description', strip_tags($document->getDescription()));
		}

with
// Set Facebook meta data
		if($params->get('facebookMetatags', '1'))
		{
			$document = JFactory::getDocument();
			$uri = JURI::getInstance();
			$document->setMetaData('og:url', $uri->toString());

            if ($item->extra_fields[1]->value !== '')
            {
                $item->title = html_entity_decode($item->extra_fields[1]->value.' '.$item->title);
            } 
            else 
            {
                $item->title = html_entity_decode($item->title);
            }

 		$item->title = $this->escape($item->title);

			$document->setMetaData('og:title', $item->title);
			$document->setMetaData('og:type', 'article');
			$facebookImage = 'image'.$params->get('facebookImage', 'Small');

			$altText = $item->extra_fields[0]->value;
			$altText = str_replace("k2","k2-og",$altText);
$document->setMetaData('og:image', $altText);
			

			$document->setMetaData('og:description', htmlspecialchars(strip_tags($document->getDescription()), ENT_QUOTES, 'UTF-8'));
		}

It's pulling the correct extra field but in the actual page is showing
<meta property="og:title" content="Series and Chapters Title" />
	<meta property="og:type" content="article" />
	<meta property="og:image" content="&lt;img src=&quot;/images/k2-og/faceApprPic.jpg&quot; alt=&quot;Image&quot; /&gt;" />

instead of
<meta property="og:image" content="< img src="/images/k2-og/faceApprPic.jpg" alt="Image" />" />
[/code]

I have tried
$document->setMetaData('og:image', html_entity_decode($altText, ENT_QUOTES));
but this doesn't work
when I try
$document->setMetaData('og:image', htmlentities($altText));
I get result
<meta property="og:image" content="&amp;lt;img src=&amp;quot;/images/k2-og/faceApprPic.jpg&amp;quot; alt=&amp;quot;Image&amp;quot; /&amp;gt;" />
Last edit: 7 years 10 months ago by popper.

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 10 months ago #163102 by Krikor Boghossian
Replied by Krikor Boghossian on topic Using Extra Field Image for og:image
This is considered a hack and I highly recommend against it.

A simple (template) solution would be based on this, just change the meta type -> github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L176-L184

If you are using an image extrafield then this will be useful
getk2.org/documentation/tips-a-tricks/1129-extra-field-image-type-as-link

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

  • popper
  • popper's Avatar Topic Author
  • Offline
  • New Member
More
7 years 10 months ago #163133 by popper
Replied by popper on topic Using Extra Field Image for og:image
Yeah I know it's a hack, which is considered bad form, but I did figure out the problem, just by more hacking. So if anyone else wants to use this very ill advised solution here it is
// Set Facebook meta data
		if($params->get('facebookMetatags', '1'))
		{
			$document = JFactory::getDocument();
			$uri = JURI::getInstance();
			$document->setMetaData('og:url', $uri->toString());

            if ($item->extra_fields[1]->value !== '')
            {
                $item->title = html_entity_decode($item->extra_fields[1]->value.' '.$item->title);
            } 
            else 
            {
                $item->title = html_entity_decode($item->title);
            }

 		$item->title = $this->escape($item->title);

			$document->setMetaData('og:title', $item->title);
			$document->setMetaData('og:type', 'article');
			//$facebookImage = 'image'.$params->get('facebookImage', 'Small');

			$altText = $item->extra_fields[0]->value;
			$altText = str_replace("k2","k2-og",$altText);	
			$altText = str_replace('<img src=',"",$altText);
			$altText = str_replace(" alt=","",$altText);
			$altText = str_replace(" />","",$altText);
			$altText = str_replace("Image","",$altText);
			$altText = str_replace('"',"",$altText);

			$image = $altText;
			$document->setMetaData('og:image', 'http://mysite.net'.$image);
			$document->setMetaData('image', $image);
			$document->setMetaData('og:description', htmlspecialchars(strip_tags($document->getDescription()), ENT_QUOTES, 'UTF-8'));
	}

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 10 months ago #163152 by Krikor Boghossian
Replied by Krikor Boghossian on topic Using Extra Field Image for og:image
Actually this can be moved in the item.php file of your overrides without much hassle and with the exact functionality.
So before you update K2 next you might look into porting this in your template.

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


Powered by Kunena Forum