Laravel - social media share buttons with custom content
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Social Sharing: Customizing Content for Facebook and Google+
As developers building modern web applications, we often focus on the functionality of our backend, but the experience a user has when sharing content across social media platforms is equally crucial. When you share a link, the platform uses metadata (title, description, image) to create a preview card. If this metadata is missing or incorrect, the resulting share looks unprofessional and fails to engage the audience.
You’ve hit on a very common pain point: standard sharing links often default to whatever information they can scrape from your page, which rarely matches the carefully crafted content you intend to share.
This post will dive deep into how you can take complete control of what gets shared by implementing Open Graph and Twitter Card protocols, specifically within the context of a powerful framework like Laravel.
The Limitation of Basic Sharing Links
The basic methods you are currently using—such as direct links to sharer.php or simple URL sharing—rely on the social platform’s internal scraping mechanisms. While convenient, these mechanisms often prioritize simplicity over customization. Facebook and Google+ (and now other platforms) rely heavily on structured data embedded in your HTML to generate rich previews. If that data isn't explicitly provided, they fill in the blanks with default or scraped information, leading to the inconsistencies you are observing with titles and images.
To achieve true customization, we must stop relying on external scripts and instead embed the necessary instructions directly into your page’s HTML using Open Graph (OG) tags and Twitter Cards.
The Developer Solution: Open Graph Protocol
The solution lies in defining specific meta tags within the <head> section of your HTML document. These tags act as a contract, telling social media crawlers exactly what image, title, and description they should use when previewing your link.
Essential Open Graph Tags
For maximum compatibility across platforms like Facebook, LinkedIn, and others, these are the most critical tags:
<meta property="og:title" content="Your Custom Page Title Here">
<meta property="og:description" content="A compelling description of your shared content.">
<meta property="og:image" content="https://www.yourwebsite.com/images/share-thumbnail.jpg">
<meta property="og:url" content="http://www.yourwebsite.com/your-page-slug">
<meta property="og:type" content="article">
og:title: The title that will appear in the share preview.og:description: The summary text accompanying the link.og:image: The URL of the image Facebook/Google+ will use for the thumbnail (this is crucial for stopping default images).og:url: The canonical URL of the content being shared.
Implementing Custom Sharing in Laravel
Since you are likely using a robust framework like Laravel, dynamically generating these tags based on your database content is the most efficient approach. This keeps your presentation logic centralized and clean.
You can use Blade templating to inject these dynamic properties into your layout file:
<head>
<title>{{ $page->title ?? 'Default Title' }}</title>
{{-- Open Graph Tags for Facebook, LinkedIn, etc. --}}
<meta property="og:title" content="{{ $page->seo_title }}">
<meta property="og:description" content="{{ $page->seo_description }}">
<meta property="og:image" content="{{ asset('images/share_' . $page->slug . '.jpg') }}">
<meta property="og:url" content="{{ route('pages.show', $page->slug) }}">
<meta property="og:type" content="article">
{{-- Twitter Card Tags (for Twitter integration) --}}
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="{{ $page->seo_title }}">
<meta name="twitter:description" content="{{ $page->seo_description }}">
<meta name="twitter:image" content="{{ asset('images/share_' . $page->slug . '.jpg') }}">
</head>
This method ensures that whenever a user shares a link from your Laravel application, the social media platforms receive precisely the information you have defined. This approach mirrors best practices in building scalable web applications, much like how you structure Eloquent models when working with Laravel data.
Beyond Basic Tags: Using Packages
While manually managing these tags works perfectly, if you manage hundreds of dynamic pages, creating and maintaining this logic can become tedious. Fortunately, the community offers excellent packages designed to automate this process for you. Searching for social sharing packages often leads to solutions that handle the complex logic of generating both Open Graph and Twitter Card data automatically based on your route or controller context.
Conclusion
Stop relying on default sharing behavior. By mastering the Open Graph protocol and implementing these specific meta tags dynamically within your Laravel application, you gain complete control over how your content is presented when shared. This shift from passive scraping to active metadata definition transforms a basic link into a rich, engaging social media preview, significantly improving your content distribution strategy.