Keyword

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

  • George Nasis
  • George Nasis's Avatar Topic Author
  • Offline
  • Elite Member
More
5 years 7 months ago #171667 by George Nasis
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.

More
5 years 7 months ago #171731 by JoomlaWorks
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

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

  • George Nasis
  • George Nasis's Avatar Topic Author
  • Offline
  • Elite Member
More
5 years 7 months ago - 5 years 7 months ago #171846 by George Nasis
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: 5 years 7 months 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
5 years 7 months ago - 5 years 7 months ago #171847 by George Nasis
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: 5 years 7 months ago by George Nasis.

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

More
5 years 7 months ago #171853 by JoomlaWorks
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. ;)

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

  • George Nasis
  • George Nasis's Avatar Topic Author
  • Offline
  • Elite Member
More
5 years 7 months ago #171857 by George Nasis
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.

More
5 years 7 months ago #171858 by JoomlaWorks
True :)

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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


Powered by Kunena Forum