News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Need help to add facebook open graph in smf 2.0.11

Started by Pravi, July 14, 2016, 06:49:55 AM

Previous topic - Next topic

Pravi

Hello guys,

     Need little help to add facebook opengraph to smf. Facebook not showing any pictures when copying links to facebook. I just want to pop up an image when links from my site is copied to facebook. Tried some mods ...still no hope.
Need some serious help guys.

Thank you

Pravi


Gluz

Do you want just one image for all of the content of your site shared on Facebook, or want a different image (that is in the post) to be the image in Facebook?

Each option have different method to do it, so, you need to clarify.

Pravi

Quote from: Gluz on July 15, 2016, 01:00:22 PM
Do you want just one image for all of the content of your site shared on Facebook, or want a different image (that is in the post) to be the image in Facebook?

Each option have different method to do it, so, you need to clarify.

Hello Gluz,

First of all thanks for the replay.
I will go with the second option. Its not necessary to have pictures in all topic. In that case i want to place a permanent image for topic without images. Any image can be taken from topics, having images.
I  am tried different mods and edited different templates ..but nothing workd...


Gluz

Well, you need to upload a default image for your forum, and then edit this files:



/Themes/your_theme/index.template.php
Search:
// Output any remaining HTML headers. (from mods, maybe?)
Replace with:

// OG image
if (isset($context['og_image']))
echo '<meta property="og:image" content="' . $context['og_image'] . '" />';
else
echo '
<meta property="og:image" content="url of your default forum image for topics with no image at all" />';

// Output any remaining HTML headers. (from mods, maybe?)




/Sources/Display.php
Search:
// Get all the important topic info.
$request = $smcFunc['db_query']('', '
SELECT
t.num_replies, t.num_views, t.locked, ms.subject, t.is_sticky, t.id_poll,

Replace with:
// Get all the important topic info.
$request = $smcFunc['db_query']('', '
SELECT
t.num_replies, t.num_views, t.locked, ms.subject, t.is_sticky, t.id_poll, ms.body,


Search:
// Add up unapproved replies to get real number of replies...
Replace with:
// OG image
$aux = preg_match_all('/\[img.*\](.+)\[/img\]/i', $topicinfo['body'], $aux2);
if (!empty($aux2[1][0]) && !isset($context['og_image']))
$context['og_image'] = $aux2[1][0];

// Add up unapproved replies to get real number of replies...




And this should make your forum set the image for Facebook and any other page that uses OpenGraph.

Pravi

Hi Gluz,

       Thank you very much..I tested it just a while ago and  the code works fine.
Is there any way i can choose image from topic, if the topic contains an image.

No image in topic---->choose default image.
if any image in topic----> choose that particular image.

Thanks a lot 

Gluz

With that code, it selects the first image in the post. If no image it uses the default image. Did you edit the image URL in the code of index.template.php?

Pravi

Quote from: Gluz on July 18, 2016, 09:22:19 PM
With that code, it selects the first image in the post. If no image it uses the default image. Did you edit the image URL in the code of index.template.php?
Hello Gluz,

The default image is working fine, but the image from post is not being fetched by facebook.  We will solve this problem later.
Right now i having a another problem, facebook only fetches the image, when links of the topics are shared twice. The problem  solved when i add og.url tag using the following code in index.template.php.

     echo ' <meta property="og:url" content="http://mysite.com" />'

After adding the above code, facebook fetches the og.tittle and og.description from the main index page of my site.

I need some help to get og.tittle  from the topic tittle and og.description from from the first post.
I tried different meathods, but none of them work...

Need some help..

Thank You

Gluz

Sorry for the late reply, I've been busy the last couple of weeks.

You just need to change this part in the og:url code:
echo ' <meta property="og:url" content="', $context['canonical_url'], '" />'

That will send the right URL to Facebook or whatever that uses the OpenGraph meta tags.

Pravi

HI Gluz,

         I tried as you have mentioned. But  facebook fetches the og:image only when shared twice. When shared for the first time, facebook doest fetch image, when shared the second time, facebook fetches the image. I dont know weather its bug with facebook scapper...

Gluz

Yup, the first time facebook doesn't load the image.

Advertisement: