Facebook share button code snippet

Stefan Bogdanescu
Founder & Senior Architect · 13 September 2026

Creating a Facebook Share Button for Your Laravel Application

In today’s interconnected world, social sharing buttons have become an essential part of any modern web application. They allow users to easily share content on their favorite platforms with just a single click. In this post, I’ll guide you through the process of adding a horizontal Facebook share button to your Laravel application.

Step 1: Include the Facebook SDK

The first step is to include the official Facebook JavaScript SDK in your project. This will provide us with all the necessary functionality for sharing content on Facebook. You can add this script to your resources/js/app.js file or include it directly in your blade template using a script tag.

<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js"></script>

Step 2: Initialize the SDK

Next, we need to initialize the Facebook SDK. We’ll use this initialization to configure our share button. You can add the following JavaScript code after including the script tag:

window.fbAsyncInit = function() {
    FB.init({
        appId      : 'YOUR_APP_ID',
        cookie     : true,
        xfbml      : true,
        version    : 'v12.0'
    });
};

Replace 'YOUR_APP_ID' with your actual Facebook App ID, which you can find in the Facebook Developers portal after creating a new app.

Step 3: Create the Share Button

Now that we have initialized the SDK, let’s create our horizontal share button. We’ll use the FB.ui method to display a custom dialog for sharing content on Facebook.

function shareOnFacebook(url) {
    FB.ui({
        method: 'share',
        href: url,
    }, function(response){
       if (response && !response.error_code) {
           alert('Post was published.');
       } else {
           alert('Error occurred while publishing post!');
       }
   });
}

This shareOnFacebook function takes a URL as an argument, which represents the content you want to share. When called, it opens a dialog box with the horizontal version of the Facebook share button.

Step 4: Add the Button to Your Laravel Template

Finally, we can add this button to our Laravel blade template. For example, in your resources/views/home.blade.php file, add the following HTML:

<a href="#" onclick="shareOnFacebook('{{ route('your.route') }}'); return false;">
    Share on Facebook
</a>

Replace '{{ route('your.route') }}' with the actual URL you want to share. This can be a specific page in your application or an external link.

That’s it! Now when users click on the “Share on Facebook” link, they will see a dialog box with the horizontal Facebook share button that allows them to easily share your content.

Remember, the key is to initialize the SDK and use the FB.ui method with the correct parameters. You can find more information about customizing the appearance of the share button in the official Facebook Sharing documentation.

At Laravel Company, we specialize in helping businesses like yours build robust and scalable web applications using Laravel. If you’re looking to enhance your application’s social sharing capabilities or need assistance with other development tasks, don’t hesitate to reach out to us at https://laravelcompany.com/. Our team of experienced developers is always ready to help.

Happy coding!

Our network