WPTI.PS
WordPress Tips
  • Home
  • Functions
  • News
  • Plugins
  • Templates
  • CSS
  • Themes
  • Resources
  • About
Home » Templates » Use Template Part to add social media sharing buttons to your site
This post was written more than a year ago and might not be entirely accurate anymore.

← Google Plus to WordPress
WordPress 3.3 Dashboard Tweaks plugin →

Use Template Part to add social media sharing buttons to your site

2
Posted on October 30, 2011 in Templates
Use Template Part to add social media sharing buttons to your site

add social media buttons to your site

 

There is a huge number of different plugins available to add social media sharing buttons to your site. But why use a plugin that has more than hundred different buttons when you’re only going to use a handful of them?

Here on WPTIPS, we only use our own shortlink and the Google+ button and before June 2011, we even didn’t have that sharing button. We have chosen for this minimal approach, because we think that if you want to share our tips, you are very capable of doing that with our shortlink or using your own choice of social media. We like Google+ and it’s the only social media we actively use at the moment, so that is also the only button that made it on our site.

Of course it also depends on what audience you target with your website. Some people are not so tech-savvy and for them simply clicking a button is easier than the DIY approach.

And for sites with such a target audience the following tip might come in handy. For this tip we only focus on adding four social media sharing buttons, namely the ones of Facebook, Twitter, Google+ and LinkedIn. If you want, you can easily extend it to more than those four.

To add these four social media sharing buttons to your site, we don’t use a plugin. Instead we use a template part.

From the WordPress Codex:

A template part makes it easy for a theme to reuse sections of code and an easy way for child themes to replace sections of their parent theme.

So basically you can use a template part for anything that you can reuse within a theme and even among different themes.

In case you think that never before you have used template parts, then we can tell you that most likely you actually have used them already. For example loop-single.php (Twenty Ten) and content-single.php (Twenty Eleven) are both template parts!

Take a look at the code below and although it has clear instructions in the top of the file, we list them here too for your convenience.

    Instructions:

  • Step 1. Take the code from line 10-36 of this file, paste them into a blank file, save that file as icons-social.php and upload it to your theme’s directory. On lines 19 and 21 you need to fill in your Twitter username and on line 24 you need to upload your favourite tweet-button (or grab it from http://a2.twimg.com/a/1319826270/images/goodies/tweetn.png) to the images folder of your theme.
  • Step 2. Take line 39 of this file and paste it into your template files on the place where you want the icons to show up.
  • Step 3. Take the scripts of line 42-49 and paste them in your footer.php file just above the closing tag.
  • Step 4. Style through CSS. NOTE that with the code below you will get 4 working share buttons. If you however want to make adjustments then visit the following pages: Facebook , Twitter, Google+, LinkedIn (all links go to developers pages of the resp. sites)

Inspired by Yoast and we got to write this tip due to a recent LinkedIn WordPress Group discussion.

Which approach do you take towards adding social media sharing buttons to your site?

  • +1
  • Tweet
  • Like
Shortlink to share or email this WP Tip to your friend(s):

Back to top ↑

← Google Plus to WordPress
WordPress 3.3 Dashboard Tweaks plugin →

  • WordPress
  • Google+

2 Responses to Use Template Part to add social media sharing buttons to your site

  1. sara fields said:
    November 11, 2011
    12:52hr

    This is great! Exactly what I’ve been looking for but it is “intermediate” wordpress level. Can you help with some more detailed step-by-step?

    For example: “Step 2. Take line 39 of this file and paste it into your template files on the place where you want the icons to show up.”

    Not a clue what/where to start with that…

    • WPTIPS said:
      November 11, 2011
      13:13hr

      Hi Sara, thanks for your comment. If you were to take the entire code and paste into your favourite text editor, you would see that line 39 is the line that says < ?php get_template_part( 'icons', 'social' ); ?>. With that line you call the template part. So in other words, that line will show the social media sharing buttons. Now obviously it depends on you where you want to show those icons, but a fairly logical place would be on your single posts, hence in the single.php template.
      If you cannot figure it out, you can hire me for some WordPress Consultation/Coding.

 WP TIPS on Google+

 RSS Links

  • Subscribe to WP TIPS by Email
  • All Tips
  • All comments

 WP TIPS Recommends

sponsor

sponsor

sponsor

sponsor

 Popular WP TIPS

  • Replace/Remove Default Header Image Twenty Eleven Theme
  • 4 ways on dealing with vendor prefixes
  • Replace Default Header Image Twenty Ten Theme
  • Make Your Own Latest News Dashboard Widget
  • Creating a Member List
  • Playing with Post Formats of WordPress 3.1
  • How to add default custom background image WordPress 3.4?
  • Extending the WordPress Walker Class
  • Multisite Dashboard Feed Widget plugin
  • WordPress 3.3 Dashboard Tweaks plugin

 Donations are Welcome!

I would like to donate

 Latest WP TIPS

    This post was written more than a year ago and might not be entirely accurate anymore.

  • Plugin Challenge
  • Prefix free by Lea Verou
  • Elegant Themes WPML compatible
  • Beijing WordPress Meetup Widget
  • Tip on how to add plugin directories to mu-plugins folder

WordPress Services

  • WordPress Consulting
  • WPML Assessment
  • Mobile Solutions
  • SEO & Social Media Strategy
  • Website Development
  • Managed WordPress Hosting

Go to top ↑

© Copyright 2013 · WP TIPS · Basejump WordPress Theme developed by Senlin Online · Powered by WordPress