Social media sharing is a powerful way to boost engagement and drive traffic to your website. With a custom social media post-sharing code, you can make it easy for your readers to share your content across various social media platforms. In this blog, we will discuss how you can create your own custom social media sharing code using PHP.
The code provided below demonstrates how to construct sharing URLs for different social media platforms without using any script. It uses PHP functions to get the current page URL, title, and thumbnail image to generate sharing links for Facebook, Twitter, Pinterest, LinkedIn, and Google+.
<?php
$crunchifyURL = urlencode(get_permalink());
$crunchifyTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8');
$crunchifyThumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
$twitterURL = 'https://twitter.com/intent/tweet?text=' . $crunchifyTitle . '&url=' . $crunchifyURL;
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u=' . $crunchifyURL;
$googleURL = 'https://plus.google.com/share?url=' . $crunchifyURL;
$bufferURL = 'https://bufferapp.com/add?url=' . $crunchifyURL . '&text=' . $crunchifyTitle;
$linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url=' . $crunchifyURL . '&title=' . $crunchifyTitle;
$pinterestURL = 'https://pinterest.com/pin/create/button/?url=' . $crunchifyURL . '&media=' . $crunchifyThumbnail[0] . '&description=' . $crunchifyTitle;
echo "<div class='media-bar'>
<ul>
<li class='ml-2'><a href='$facebookURL' target='_blank'><i class='media-icon fab fa-facebook-f'></i></a></li>
<li class='ml-2'><a href='$twitterURL' target='_blank'><i class='media-icon fab fa-twitter'></i></a></li>
<li class='ml-2'><a href='$pinterestURL' target='_blank'><i class='media-icon fab fa-pinterest'></i></a></li>
<li class='ml-2'><a href='$linkedInURL' target='_blank'><i class='media-icon fab fa-linkedin-in'></i></a></li>
</ul>
</div>";
?>
You can modify the code to add additional social media platforms or customize the styling of the sharing buttons to match your website’s design.
Conclusion
Social media sharing is an effective way to increase traffic and engagement on your website. By creating custom social media post sharing code using PHP, you can provide an easy way for your readers to share your content across various social media platforms. The code above can be modified and customized to suit your website’s needs and design.