Hey! Are you looking for a plugin for the social share button? No way, You can do it simply with PHP code. There are so many WordPress plugins for social share buttons. But If you want to “Create Social Share Button in WordPress without Plugin”. Stay with us, We will show it.
When you are using a WordPress theme you can not use the main theme function.php file for writing your own code. So, The solution is you can create a child theme for your own customization. It’s better to use a child theme for writing your own code.
Why Important Social Share Buttons:
In the present time, Most people in the world use social media platforms. Customers are manipulated in many ways. Social media is one of them. People take help from social media for every single problem. It’s part of everyday life. And they like to share with their friend what he or she likes or has a problem with. We can use it.
Think about it, Someone reads your article and he or she likes it. And They want to share your article with their friends. If they share it, You will get visitors and customers. But if you do not have social share buttons. They can not think about sharing this article with their friend instantly. If you have social share buttons on your post. As a result, You will increase your social media sharing rate.
We think Every blog or any type of website needs a social share button.
Create Social Share Button in WordPress without Plugin:
As usual, we need social sharing links for blog posts. So, We will create a social share link for blog posts. We will create a function that creates a social share link and that link will automatically be added below the blog posts. We are using the_content filter.
Use this code on function.php:
function wpk_soical_share_buttom($content){ global $post; if (is_singular() || is_home()) { //get shareable url & title & image Url $post_url = urlencode(get_permalink()); $post_title = str_replace( ' ', '%20', get_the_title()); $thumbnail = get_the_post_thumbnail_url($post->ID); $twitter = 'https://twitter.com/intent/tweet?text='.$post_title.'&url='.$post_url.'&'; $facebook = 'https://www.facebook.com/sharer.php?u='.$post_url; $linkedIn = 'https://www.linkedin.com/shareArticle?mini=true&url='.$post_url.'&title='.$post_title; if ($thumbnail != '') { $pinterest = 'https://pinterest.com/pin/create/button/?url='.$post_url.'&media='.$thumbnail[0].'&description='.$post_title; }else{ $pinterest = 'https://pinterest.com/pin/create/button/?url='.$post_url.'&description='.$post_title; } $content .= '<div> <a class="share-ico ico-facebook" href="'.$facebook.'" title="Share on facebook" target="_blank" rel="nofollow">Share</a> <a class="share-ico ico-twitter" href="'.$twitter.'" title="Tweet on Twitter" target="_blank" rel="nofollow">Tweet</a> <a class="share-ico ico-pinterest" href="'.$pinterest.'" title="Pin on Pinterest" target="_blank" rel="nofollow">Pin it</a> <a class="share-ico ico-linkedin" href="'.$linkedIn.'" title="Share on LinkedIn" target="_blank" rel="nofollow">Post</a> </div>'; return $content; } } add_filter( 'the_content', 'wpk_soical_share_buttom', 50);
Note: Facebook share link does not work in Localhost.
Oh, If you want more social media share links let us know. We will implement your request. We added some CSS for design purposes.
Add CSS on style.css:
a.share-ico.ico-facebook { background-color: #3b5998; color: #fff; } a.share-ico.ico-twitter { background-color: #00a9ed; color: #fff; } a.share-ico.ico-pinterest { background-color: #c8232c; color: #fff; } a.share-ico.ico-linkedin { background-color: #0e76a8; color: #fff; } a.share-ico { width: 140px; height: 35px; font-size: 1rem; line-height: 35px!important; text-align: center; margin-right: 6px; display: inline-block; -webkit-border-radius: 4px; -moz-border-radius: 4px; -o-border-radius: 4px; border-radius: 4px; text-decoration: none; }
Note: We are not adding icons, You can add related icons. But if you can not add an icon, please comment below.
Conclusion:
Social sharing is the most important part of blogging. You can use any social sharing plugin for this job. But we will prefer to try to write code yourself. And if you working with custom projects or Development own WordPress theme this will be important for you.
If you like this article or have any problem with it, please comment below. We will be ready to help you.
Hello !
Thank you for sharing this code.
1 – How do you do to exclude all pages of a WordPress website ?
2 – Or just some of them, like “contact-page” AND “privacy-policy-page” ?
Best Regards,
Oly
Thanks for the comment.
I am working on that.
Hello! I’m at work surfing around your blog from my new iphone! Just wanted to say I love reading through your blog and look forward to all your posts! Carry on the great work!
Thanks, It really encouraging for us.
You are my intake, I own few blogs and infrequently run out from brand :). “No opera plot can be sensible, for people do not sing when they are feeling sensible.” by W. H. Auden.
Appreciate it for this howling post, I am glad I observed this website on yahoo.
Appreciate you sharing, good article.Thanks Again. essentially Cool.
Hiya very nice blog!! Guy .. Beautiful .. Wonderful .. I will bookmark your website and take the feeds alsoKI am happy to seek out numerous helpful information here within the put up, we’d like develop more strategies in this regard, thanks for sharing. . . . . .
Thank you. That works great. I have connected your code on local hosting. The Facebook button works too. But without icons, the buttons don’t get any attention. Please add the code for the buttons with icons. And add the Telegram and WhatsApp buttons. And one more thing. Your code added me buttons not only in the entry, but also in the archives under the topmost entry. Is it possible to remove it from there how?