Laravel - social media share buttons with custom content

14,756

something like this for facebook share

<meta property="og:title" content="title here" />
<meta property="og:image" content="image url here" />
<meta property="og:type" content="website" />

debug your page first on facebook using https://developers.facebook.com/tools/debug/ to clear facebook cache on your site

you don't have to create app on facebook to use share button, i think it's same with gplus but i'm not sure.

or create this page as a target url to share

<php 
    $title = 'title';
    $image = 'image';
?>
<meta property="og:title" content="{{$title}}" />
<meta property="og:image" content="{{$image}}" />
<meta property="og:type" content="website" />
<script>
    window.location = "http://www.url-to-real-shared-page";
</script>
Share:
14,756
omarsafwany
Author by

omarsafwany

Updated on June 26, 2022

Comments

  • omarsafwany
    omarsafwany almost 2 years

    How can I customize the content display when I share a page on facebook or google plus. title, description, image..etc.. When I share my page now on facebook, it adds a certain image and title other the required. In case of google plus, image is shared correctly but still the title is not the one I need. Here's how I currently use them:

    <a href="http://www.facebook.com/sharer.php?u=http://www.mywebsite.com" target="_blank"></a>
    
    <!-- Google+ -->
    <a href="https://plus.google.com/share?url=http://www.mywebsite.com" target="_blank"></a>
    

    Any idea how can I customize it to my need or if there is any packages that could support me as well. Also, do I need to create an app on facebook or google plus in order to do so. I'm just using share button.

    • morgan9999
      morgan9999 over 9 years
      i think the title and image for facebook share is determined by meta tag of the page shared, also for gplus, but i'm not sure for gplus.
    • Renny M. Roy
      Renny M. Roy over 6 years
      Solution is very well explained here.. stackoverflow.com/questions/34368156/…
  • omarsafwany
    omarsafwany over 9 years
    The problem is that the head tag where all of the above should be placed is in a layout that's loaded in my view. Also the content in my view gets changed dynamically, so how can change the og data each time I want to share?!
  • morgan9999
    morgan9999 over 9 years
    i did that too before and i couldn't find an answer. in the end what i did is create a blank page with only meta tags in it, and script to redirect to the real page. the meta content can be filled dynamically using data from url. i know this is not the best way to do it though, but it works for me.
  • omarsafwany
    omarsafwany over 9 years
    can you elaborate more please?!
  • morgan9999
    morgan9999 over 9 years
    i edit my answer, you can pass 'title' and 'image' variable from url
  • Khan Shahrukh
    Khan Shahrukh over 9 years
    you can have your ogs dynamic too just pass the value from controller to your view and use og:title="{{$ogtitle}}"