Recently I developed a plugin named fast social shares and after posting an article about it, many of my readers contacted me asking how to add social media sharing buttons in wordpress without using plugins.

My addon is mobile responsive and uses no JavaScript. It has clean and commented codes and is really easy to customise. In simple terms, it makes your site fast. But still, it’s a plugin.

So in this tutorial, you will learn to add social media buttons in your wordpress blog without using a plugin.

[full bg=”#1e2d33″][/full]

If you also have any question regarding web or wordpress development, then please feel free to contact me, and I will try my best to sort out your problem.

WordPress Social Media Share Buttons Without Plugin

Now let us get back to the topic and start with developing the PHP function. This function will help us to define the social buttons and to push them into the content area.

Step 1 – PHP function

I am requesting you please follow the step as listed below. In case you fail to follow any of the steps provided below, your site may go offline temporarily. I am not saying this to scare you.

If you are editing your live website then I will suggest keeping your FTP connected to your server and to also keep a backup of your original theme’s function.php file. If anything goes wrong, just replace the file and your site will get live instantly.

First you will notice a function get_the_post_thumbnail_src($img) which has $img as the parameter. I am actually performing the preg_match in order to find the thumbnail of the post.

Then you will notice that I have declared a new variable named $sb_url which I am using to store the link to the post. I am doing using urlencode(get_permalink()); function.

$sb_title is the next variable which is being used to store the post title. I have used it to dynamically generate the social share title. I am using $sb_thumb to store the information about the thumbnail of the post. I am also conditionally checking if the post thumbnail is available or not with help of if-else statement and by performing the check on!empty($sb_thumb) function.

Various other variables are used to store the URL structure of the corresponding social media sites because each of these sites has their own way to automatically generate the share links.

Finally, I have used the_content to hook this function to the content area. The complete PHP code is pretty self-explanatory and is easy to customise.

You can also use [social] shortcode anywhere on your website to show the share button. If you want to add more buttons, then you just need to add more social media named variable and proper URL parameter.

You must have noticed that not a single URL is calling any parent JavaScript file, but is still 100% functional and dynamic.

After placing these codes in your theme file, if you load any post then you will social media links at the bottom of the post. So it means that our functions are working properly. And now you just need to give some great styles to those social links.

Step 2 – Design social buttons with CSS

Add these styles to your theme’s stylesheet.css file. I am assuming that you are using font awesome fonts on your site. You will need it because the style is designed considering its font’s Unicode. You can obviously change it.

After adding these styles clear your cache and also purge your cdn cache if you are using maxcdn, cloudflare or similar service.

Why my function is better than other plugins?

If you use any social media plugin except my fast social share, then each of them loads JavaScript files from the official website. For example, every Facebook or twitter button which loads up on your web pages, generates the request to the official query server and then gather the data and then show how many likes or tweets a specific post has received.

But since my function and my plugin don’t use any JavaScript, you will notice that they load up instantly along with your main content.

If you face any problem, then don’t forget to contact me for free support.