In today’s connected digital world, giving your website visitors the ability to share content quickly is a huge advantage. When you implement Social Share Buttons in PHP, users can post your content directly to their favorite social media platforms with a single click. This simple yet powerful integration can help boost your website’s visibility, engagement, and reach in just a few minutes.
How Social Sharing Links Work
Social share buttons work by passing the current page’s URL and some additional parameters (like title or description) to a predefined sharing URL of each social platform. Each platform has its own way of accepting this data, which is usually done through query parameters.
PHP can dynamically fetch the URL of the current page and embed it in these sharing links, giving you fully functional Social Share Buttons in PHP that adapt to any of your site’s pages.
Getting Current URL In PHP
To generate our share links, we need the current URL of the page. Here’s a quick snippet to achieve that:
$currentURL = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
This code helps PHP automatically detect the page URL, making our social share buttons dynamic and accurate regardless of which page they’re used on. For better understanding, you will see example to use static URL instead of current URL in following post.
Generating Link for Social Media Sites
Before moving forward with actual social share button implementation in PHP, we will see how to create share link for different platform. We’ll create a separate PHP class file that contains a class dedicated to generating share links for each platform like Facebook, twitter(x), LinkedIn, Whatsapp, Email, Thumbler and so on.
Let’s create a new PHP file with name SocialShare.php. Inside this class, each platform will have its own static method.
<?php
class SocialShare {
// All platform share link methods will be added here
}
?>
Facebook Social Sharing
Facebook remains the go-to platform for sharing stories, news, and blog posts with a vast audience. The Facebook share link allows users to quickly post URLs with optional captions or quotes to their timeline, connecting your content to millions.
public static function facebook($url, $title): string
{
return "https://www.facebook.com/sharer/sharer.php?u=" . urlencode($url) . ""e=" . urlencode($title);
}
Twitter (X) Social Sharing
Twitter, now officially known as X, is still all about quick sharing and instant reach. Whether it’s an article, a quote, or a snippet, users can tweet your link instantly with this short and dynamic share link structure that X currently supports.
public static function twitter($url, $title)
{
return "https://x.com/intent/post?url=" . urlencode($url) . "&text=" . urlencode($title);
}
LinkedIn Networking Share
LinkedIn remains a top destination for professionals. Sharing on LinkedIn allows users to highlight knowledge-based or business-centric posts that can attract valuable connections and discussions around your content.
public static function linkedin($url)
{
return "https://www.linkedin.com/sharing/share-offsite/?url=" . urlencode($url);
}
Sharing to WhatsApp Conversations
WhatsApp is ideal for instant sharing between friends, groups, or colleagues. The following link builder turns any page into a sharable link that pops right into the chat box, making your content a topic of discussion instantly.
public static function whatsapp($url, $title) {
return "https://api.whatsapp.com/send?text=" . urlencode($title . " " . $url);
}
Email Sharing
Email remains a timeless communication channel. With the below code, readers can share your link directly through their email client, perfect for formal sharing or professional recommendations.
public static function email($url, $title)
{
return "mailto:?subject=" . urlencode($title) . "&body=" . urlencode($url);
}
Pinterest Inspiration
Pinterest is where visual content shines. This link structure is perfect for image-rich pages or product showcases, letting users pin your visuals along with a custom description for their followers.
public static function pinterest($url, $media, $description)
{
return "https://pinterest.com/pin/create/button/?url=" . urlencode($url) . "&media=" . urlencode($media) . "&description=" . urlencode($description);
}
Tumblr Blogging
Tumblr is a creative hub for visual and textual storytelling. With this share link, your content can be instantly shared on Tumblr blogs, connecting you with a unique and enthusiastic audience.
public static function tumblr($url, $title)
{
return "https://www.tumblr.com/share/link?url=" . urlencode($url) . "&name=" . urlencode($title);
}
Reddit Discussions
Reddit is all about community-driven engagement. By adding this share link, you invite users to post your content in various subreddits, sparking conversations and bringing organic traffic from active online communities.
public static function reddit($url, $title)
{
return "https://www.reddit.com/submit?url=" . urlencode($url) . "&title=" . urlencode($title);
}
In just a few lines of PHP, we’ve created an adaptable set of methods for major social platforms. Each platform has a different approach and audience. Together, they give your website dynamic Social Share Buttons in PHP. These buttons are ready to reach every corner of the social web.
Example of Showing Social Share Buttons in PHP
Now that we have successfully created our SocialShare.php class containing share link generator methods for each platform, it’s time to put it into action.
In this example, we’ll use that file to show social share buttons. They will have simple styles and clear labels, like Share on Facebook or Tweet This. The goal is to show how easily you can display functional Social Share Buttons in PHP. These buttons work on any webpage. We’ll use an example link for demonstration, along with a relevant title and image.
<?php
require_once 'SocialShare.php';
$url = "http://codewolfy.com/";
$title = "Learn to Implement Social Share Buttons in PHP in Minutes";
$description = "A quick and simple tutorial on adding working social share buttons using PHP.";
$media = "http://codewolfy.com/sample-image.jpg";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example of Social Share Buttons in PHP</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
text-align: center;
background-color: #fafafa;
}
.share-container {
max-width: 700px;
margin: 0 auto;
}
.btn-share {
color: #fff;
font-weight: 600;
display: flex;
align-items: center;
justify-content: center;
gap: 6px;
}
.btn-facebook {
background-color: #1877f2;
}
.btn-twitter {
background-color: #1d9bf0;
}
.btn-linkedin {
background-color: #0077b5;
}
.btn-whatsapp {
background-color: #25d366;
}
.btn-email {
background-color: #666666;
}
.btn-pinterest {
background-color: #e60023;
}
.btn-tumblr {
background-color: #36465d;
}
.btn-reddit {
background-color: #ff4500;
}
</style>
</head>
<body>
<div class="share-container">
<h2 class="mb-4">Share This Article</h2>
<p class="mb-5">Click on any of the buttons below to share this page instantly on your favorite platform.</p>
<div class="d-flex flex-wrap justify-content-center gap-3">
<a class="btn btn-share btn-facebook" href="<?php echo SocialShare::facebook($url, $title); ?>" target="_blank">
<i class="bi bi-facebook"></i> Share on Facebook
</a>
<a class="btn btn-share btn-twitter" href="<?php echo SocialShare::twitter($url, $title); ?>" target="_blank">
<i class="bi bi-twitter-x"></i> Share on X (Twitter)
</a>
<a class="btn btn-share btn-linkedin" href="<?php echo SocialShare::linkedin($url); ?>" target="_blank">
<i class="bi bi-linkedin"></i> Share on LinkedIn
</a>
<a class="btn btn-share btn-whatsapp" href="<?php echo SocialShare::whatsapp($url, $title); ?>" target="_blank">
<i class="bi bi-whatsapp"></i> Share on WhatsApp
</a>
<a class="btn btn-share btn-email" href="<?php echo SocialShare::email($url, $title); ?>" target="_blank">
<i class="bi bi-envelope"></i> Share via Email
</a>
<a class="btn btn-share btn-pinterest" href="<?php echo SocialShare::pinterest($url, $media, $description); ?>" target="_blank">
<i class="bi bi-pinterest"></i> Share on Pinterest
</a>
<a class="btn btn-share btn-tumblr" href="<?php echo SocialShare::tumblr($url, $title); ?>" target="_blank">
<i class="bi bi-tumblr"></i> Share on Tumblr
</a>
<a class="btn btn-share btn-reddit" href="<?php echo SocialShare::reddit($url, $title); ?>" target="_blank">
<i class="bi bi-reddit"></i> Share on Reddit
</a>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- Bootstrap Icons CDN -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
</body>
</html>
In simple terms, we import our class file. Then, we use each static method to print clickable links for major social media platforms. When someone clicks a link, a new window opens. They can then share your webpage directly.

Conclusion
Within minutes, you can add Social Share Buttons in PHP. These buttons let your users spread the word about your website. Dynamic share links improve audience engagement, encourage organic traffic, and give your content a professional touch. With just a few lines of PHP, visitors can promote your work across Facebook, Twitter, LinkedIn, and other platforms easily.