How to Add Facebook Share Button in PHP Website

AuthorSumit Dey Sarkar

Pubish Date24 Feb 2024

categoryPHP

In this tutorial, we will learn how to add Facebook share button in PHP website.

How to Add Facebook Share Button in PHP Website?

Facebook Share in PHP

To add a Facebook Share button to your PHP website, you can use the Facebook Share Button plugin provided by Facebook.

Follow the below steps to add Facebook share button to your PHP website.

Step 1 : Create a Facebook App

Before adding the Share button, you need to create a Facebook App to obtain an App ID.

  • Go to the Facebook Developer website and log in.
  • Create a new app and follow the given instructions to set it up.

Once your app is created, you will get an App ID.

Step 2 : Include the Facebook SDK

Include the Facebook SDK script in the <head> section of your HTML document. You can add the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your PHP Website</title>

    <!-- Include Facebook SDK script -->
    <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v10.0&appId=YOUR_APP_ID" nonce="YOUR_NONCE"></script>
</head>
<body>

Make sure to replace YOUR_APP_ID with the actual App ID you obtained in step 1.

Step 3 : Add the Share Button

Place the following code wherever you want the Facebook Share button to appear on your page:

<!-- Facebook Share button -->
<div class="fb-share-button" data-href="https://your-website-url.com" data-layout="button_count"></div>

Replace https://your-website-url.com with the URL of the page you want to share.

Step 4 : Style the Button (Optional)

Style your Facebook share button if needed.

<style>
    .fb-share-button {
        margin: 10px;
    }
</style>

Note

Make sure your website is accessible online, as the Facebook Share button requires a public URL to work correctly.

Comments 0

Leave a comment