bug (?) in university with og:image tag

May 2014 Joomla Template
GK User
Thu Aug 25, 2016 3:43 pm
Hi,

When displaying an article with an image in the article itself, the og:image tag is NOT generated.
I have looked at the code and see that there is something not correct...

in file: ./html/com_content/article/default.php

the following code handles the og:image
Code: Select all
if (version_compare( JVERSION, '1.8', 'ge' ) && isset($images->image_fulltext) and !empty($images->image_fulltext)) {     $og_image = $uri->root() . htmlspecialchars($images->image_fulltext);
     $pin_image = $uri->root() . htmlspecialchars($images->image_fulltext);
} else {
     $og_image = '';
     preg_match('/src="([^"]*)"/', $this->item->text, $matches);

     if(isset($matches[0])) {
        $pin_image = $uri->root() . substr($matches[0], 5,-1);
     }
}[code]

it first looks for a fulltext image, if not found it goes to the else part where $og_image = set to '', but unlike $pin_image this variable is never set a value as you can see > resulting in NO og:image tag > resulting in facebook doing 'roulette' in choosing the article image :(

furthermore, setting $pin_image = $uri->root() . substr($matches[0], 5,-1); is likely to fail on systems that store the full url path in the article text and not the relative url > the $uri->root is then added while it is already present > this results in an faulty image url...

As a quick work around I have changed the code to look like this.
[code]if (version_compare( JVERSION, '1.8', 'ge' ) && isset($images->image_fulltext) and !empty($images->image_fulltext))
{
     $og_image = $uri->root() . htmlspecialchars($images->image_fulltext);
     $pin_image = $uri->root() . htmlspecialchars($images->image_fulltext);
}
elseif (version_compare( JVERSION, '1.8', 'ge' ) && isset($images->image_intro) and !empty($images->image_intro))
{
     $og_image = $uri->root() . htmlspecialchars($images->image_intro);
     $pin_image = $uri->root() . htmlspecialchars($images->image_intro);
}
else
{
     $og_image = '';
     preg_match('/src="([^"]*)"/', $this->item->text, $matches);

     if(isset($matches[0])) {
        $og_image = substr($matches[0], 5,-1);
        $pin_image = $uri->root() . substr($matches[0], 5,-1);
     }
}


this additionaly checks on an intro image being present (not only full text image) and it 'fixes' the og_image . Note that I have omitted the $uri->root() on the og_image but that is custom for my sites...

Looking forward to a formal fix :)
User avatar
Senior Boarder

teitbite
Sun Aug 28, 2016 9:41 am
Hi

There is couple of conditions there from where image can be taken. Can I please see a page You have a problem with so I'll know which method is used and run down the place where problem can be.
User avatar
Moderator

GK User
Mon Aug 29, 2016 10:49 am
Hi, I have 'fixed' the code so currently I have no page to show you :(
Is the description with code sample not sufficient enough?
User avatar
Senior Boarder

teitbite
Thu Sep 01, 2016 1:26 pm
Hi

Like I've said: I do not know which part of the code is used on Your site. In this piece of code there is couple of options and depending from the situation a different part of code is used, without an example I'm not able to tell where is the problem. Anyway I'll pass this to programmers so they will test it for different settings.
User avatar
Moderator

GK User
Thu Sep 01, 2016 1:51 pm
Thanks!
User avatar
Senior Boarder

teitbite
Sun Sep 04, 2016 1:59 pm
Hi

I was told that facebook has it's own script picking up the image when its located in content, so the only issue here is that if $og_image is empty it should not generate og:image tag at all. This should be corrected soon.
User avatar
Moderator

GK User
Thu Sep 15, 2016 2:28 pm
Hi, that is how it currently is.
when facebook doesn't find a og:image tag (or linkedin for that matter), they pick an image by themself (read: random) > this can be every image on the page (inclusive adds etc. > totally irrelevant to the page when sharing)

please understand that not every site uses the intro / full image, but images are also implemented via the article itself.

I have fixed this myself a long time ago on meet-gavern and the code was implemented.

This is not only an issue for this template: just worked on another site with a Gavick template were I was asked to fix facebook shares taking the wrong image... guess what...

Same issue: same fix!
User avatar
Senior Boarder

teitbite
Sat Sep 17, 2016 5:08 pm
Hi

Ok. So the code from Your first post has this fix included? Please confirm and I'll ask programmers to move it to templates.
User avatar
Moderator


cron
Remember me
Register New Account
If you are old Gavick user, click HERE for steps to retrieve your account.