0

In my shopping site project, I am trying to share the current link (which is product details page) on Facebook using the following url which is working fine:

<?php
    $shareUrl = urlencode($product_url);
?>
<a class="share-button-link" href="<?php echo "https://www.facebook.com/sharer/sharer.php?u=" .$shareUrl; ?>">
   Facebook
</a>

The above one is working fine but the title and image of the current sharing link on FB is coming different. I mean the image is present on the current url but I want to show some other image.

Now, the current url has many images and I think, Facebook is selecting a random image from the collection of images and title is based on something else (not sure).

My question is: How to add a specific image and title to the sharing link?

I tried this link but doesn't work for me.

<?php 
    $product_url=$ _product->getProductUrl(); 
    $shareUrl = urlencode("http://www.example.com/product-a");     
    $shareTitle = urlencode($_product->getName()); 
    $shareImage = urlencode("http://i.imgur.com/59oQPGZ.jpg"); 
?> 
<a class="share-button-link" href="https://www.facebook.com/sharer.php?s=100&amp;p[title]=<?php echo $shareTitle; ?>&amp;p[url]=<?php echo $shareUrl; ?>&amp;p[summary]=nothing+but+you&amp;p[image]=<?php echo $shareImage; ?>">
    Facebook
</a>

I can't use this:

<meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>
<meta property="og:image:secure_url" content="https://secure.example.com/ogp.jpg" />

Because the images will change for every product page. (I need change the image dynamically, which is not possible in this case)

Community
  • 1
  • 1
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
  • 2
    _Why_ can’t you use the meta elements? If your page is generated dynamically, then where is the problem in outputting the meta info dynamically as well? (Anyway, you will _have to_ use those if you want to use the share dialog, because as of now that _only_ takes the data it displays from the OG meta info of the URL that is shared. If you don’t want that, you could use the Feed dialog instead, that still allows to set this parameters dynamically [for now].) – CBroe Mar 17 '14 at 11:55
  • Do you have specific url for each content? You can do that with OG tags. If you can give further detail, I can help – Hüseyin BABAL Mar 17 '14 at 12:39
  • As CBroe mentioned it, You can use `Feed` dialog – Adam Azad Mar 17 '14 at 20:49
  • @CBroe sorry, I didn't get you.. please give a small example whenever you have time. I have solution in my hand, which I am showing in my post but don't know how to do this dynamically.. btw.. the code is in phtml template files. there the image urls is changing for each product. – Mr_Green Mar 18 '14 at 05:49
  • I don’t know _what_ you want an example for. If you are able to output an image URL in your page’s content area dynamically, then why aren’t you able to do the _same_ with the meta elements – _output them dynamically_ …? – CBroe Mar 18 '14 at 07:04

1 Answers1

0

Output your PHP variables to the HTML meta tags.

<meta property="og:image" content="<?php echo $shareImage; ?>"/>
<meta property="og:title" content="<?php echo $shareTitle; ?>"/>
<meta property="og:description" content="<?php echo $shareDescription; ?>">
Spaceship09
  • 341
  • 3
  • 8