Keyword

Select Different Image for Social Sharing from the item`s Image

  • George Nasis
  • George Nasis's Avatar Topic Author
  • Offline
  • Elite Member
More
1 month 2 weeks ago #171667 by George Nasis
George Nasis created the topic: Select Different Image for Social Sharing from the item`s Image
Hallo,

Is there anyway to select a different image to share an item on facebook from the item`s main image?

My answer is based to the fact that facebook creates a landscape ration for the image, so when I have an item with a portrait orientation then the results is quite strange.

Thank you

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

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
1 month 6 days ago #171731 by Fotis
Fotis replied the topic: Select Different Image for Social Sharing from the item`s Image
a) Create an extra field of image type to use as social sharing image. Use a descriptive extra field alias, e.g. "imgsocial".
b) Add some code in your item.php override to reset the meta tags associated with social sharing.

As an example:
<?php
/**
 * @version    2.9.x
 * @package    K2
 * @author     JoomlaWorks https://www.joomlaworks.net
 * @copyright  Copyright (c) 2006 - 2019 JoomlaWorks Ltd. All rights reserved.
 * @license    GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
 */
// no direct access
defined('_JEXEC') or die;

// Get the extra field
$imgSocial = $this->item->extraFields->imgsocial->value; // Notice "imgsocial" here which is the extra field alias

// $imgSocial produces an entire <img> html element so we need to extract the src of the image
preg_match("#src=\"(.*?)\"#i", $imgSocial, $src);
$imgage = $src[1];

// Replace the meta tag with the new custom image
$document = JFactory::getDocument();
$document->setMetaData('og:image', $image);
$document->setMetaData('image', $image); // Generic meta


If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

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

  • George Nasis
  • George Nasis's Avatar Topic Author
  • Offline
  • Elite Member
More
4 weeks 1 day ago - 4 weeks 1 day ago #171846 by George Nasis
George Nasis replied the topic: Select Different Image for Social Sharing from the item`s Image
Hallo Fotis,

I followed your instructions and created a new extra field named imgsocialbook and alias imgsocialbook.

Added the code to my item.php

$imgsocialbook = $this->item->extraFields->imgsocialbook->value;
preg_match("#src=\"(.*?)\"#i", $imgsocialbook, $src);
$image = $src[1];
$document = JFactory::getDocument();
$document->setMetaData('og:image', $image);
$document->setMetaData('image', $image);

and it is not working

I try to figure out with Open Grahp Object Debugger and the error is this one:

Object at URL ' www.example.gr/categoryA/lasttestfb ' of type 'article' is invalid because the given value 'images/fbshare/example.jpg' for property 'og:image:url' could not be parsed as type 'url'.

I should notice that I use K2 2.8
Last edit: 4 weeks 1 day ago by George Nasis.

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

  • George Nasis
  • George Nasis's Avatar Topic Author
  • Offline
  • Elite Member
More
4 weeks 1 day ago - 4 weeks 1 day ago #171847 by George Nasis
George Nasis replied the topic: Select Different Image for Social Sharing from the item`s Image
I "fixed" it by adding the absolute path in the select image field :D

It is working like a charm Fotis. You helped me once again!

Oh, one question:
If I leave empty the extra field then nothing can be used as 'image' for sharing so I modified a little the code like this:

<?php if (!empty($this->item->extraFields->imgsocialbook->value)) {
$imgsocialbook = $this->item->extraFields->imgsocialbook->value;
preg_match("#src=\"(.*?)\"#i", $imgsocialbook, $src);
$image = $src[1];
$document = JFactory::getDocument();
$document->setMetaData('og:image', $image);
$document->setMetaData('image', $image);
}
?>

Thank you very much
Last edit: 4 weeks 1 day ago by George Nasis.

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

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
3 weeks 6 days ago #171853 by Fotis
Fotis replied the topic: Select Different Image for Social Sharing from the item`s Image
Yeah, sorry for that, I forgot the path provided is relative when an absolute URL is required for these meta tags.

As for your change, you can go one step further and set a default image when no specific image is provided. ;)


If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

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

  • George Nasis
  • George Nasis's Avatar Topic Author
  • Offline
  • Elite Member
More
3 weeks 6 days ago #171857 by George Nasis
George Nasis replied the topic: Select Different Image for Social Sharing from the item`s Image
It is a pity (to be honest it is a shame) to leave an item without an image but I will keep your advice in my mind :D

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

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
3 weeks 6 days ago #171858 by Fotis
Fotis replied the topic: Select Different Image for Social Sharing from the item`s Image
True :)


If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

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

Moderators: william white

Powered by Kunena Forum