Social media share buttons are added on every website and it is also necessary to increase social traffic on the website. Social share button is a great option, having advantage of sharing the blog post on social media. Your blog posts become popular around social networking sites.
Social referral always provides direct traffic to your blog post. This can cause your blog post to reach different parts of the world and sometimes have an impact on high traffic.
If a visitor comes to your blog and if want to share that page across different social media websites, then a social share button is one of the best option.
Suggested Read: Captcha And Its Types – With Examples
But the libraries and plugins used for social share icons use heavy JS, results slow down the speed of the website.
Putting a social share button on every blog post should become a ‘duty‘ for a blogger so that his work can be elevated in the air. If you get a lot of shares, your share tool shows the actual count of shares shared and newbies who land on your blog may also get a chance to consider your blog as a popular one on the web.
In this article, we’ll create a social media share button with the help of single line code in PHP without using any external library or plugin.
The first step is to save the value of Current URL in a variable using server controlled variables in PHP.
Below is the PHP code to get the current value of URL which will store the current URL value in $url variable.
The following 3 server controlled variables are used in above PHP code:
- $_SERVER[‘HTTPS’] – The client chooses to use this, but the server returns the actual value of either empty or “on”.
- $_SERVER[‘HTTP_HOST’] – Returns the Host header from the current request.
- $_SERVER[‘REQUEST_URI’] – This contains the URI of the current page.
For Pinterest Share:-
<a title="Share to Pinterest" target="_blank" href="http://pinterest.com/pin/create/button/?url=https://techhyme.com/">Pinterest</a>
Pinterest is one of the best image sharing platforms where you can share your website link with an image.
For Linkedin Share:-
<a title="Share to Linkedin" target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&url=https://techhyme.com/">Linkedin</a>
Linkedin is an another professional platform to share the blog post and is mostly used for professional networking.
For Twitter Share:-
<a title="Share to Twitter" target="_blank" href="http://twitter.com/share?text=Go and Checkout our blog &url=https://techhyme.com/ &hashtags=techhyme,blog,tech">Twitter</a>
Twitter is an another microblogging and social networking website on which users post and interact with messages known as “tweets”.
For Facebook Share:-
<a title="Share to Facebook" target="_blank" href="http://www.facebook.com/sharer.php?u=https://techhyme.com/">Facebook</a>
Facebook is a social networking website based in California where users can create a profile revealing information about themselves and can even post text, photos and multimedia.
In above examples, we used FONT Awesome Library for displaying the social share buttons but you can use any icon library of your choice.
If you have any other doubt or suggestion, you can comment below. We are also eager to know about bugs and other troubles so that we can improve ourselves with your feedback.
You may also like:- How To Get Image Information in PHP
- Integrating Elasticsearch with Node.js – A Complete Guide
- 22 Useful JavaScript Functions You Need To Know
- CSS3 nth-child Selector – A Comprehensive Guide
- PHP Loops – A Comprehensive Guide
- Different Types of Functions in PHP
- Various String Types in PHP – Utilizing ctype Functions
- Understanding Conditional Statements in PHP
- Mastering PHP Arrays – A Comprehensive Guide
- Exploring Strings in PHP – A Comprehensive Guide