Post Pic

Automatically add Twitter and Facebook buttons to your posts

Nowadays, most bloggers are using Facebook and Twitter to promote their posts. In today’s recipe, I’m going to show you how you can easily add Twitter and Facebook buttons to the bottom of your posts.

Paste the code below into your functions.php file, save it, and you're done.

function share_this($content){
    if(!is_feed() && !is_home()) {
        $content .= '<div class="share-this">
                    <a href="http://twitter.com/share"
class="twitter-share-button"
data-count="horizontal">Tweet</a>
                    <script type="text/javascript"
src="http://platform.twitter.com/widgets.js"></script>
                    <div class="facebook-share-button">
                        <iframe
src="http://www.facebook.com/plugins/like.php?href='.
urlencode(get_permalink($post->ID))
.'&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;colorscheme=light&amp;height=21"
scrolling="no" frameborder="0" style="border:none;
overflow:hidden; width:200px; height:21px;"
allowTransparency="true"></iframe>
                    </div>
                </div>';
    }
    return $content;
}
add_action('the_content', 'share_this');

Thanks to Dev7Studios for this awesome recipe!

7 Responses

Aug 09 2011 04:04

Is it possible for this to be above the content?

Aug 18 2011 10:06

This is a really useful bit of code. Social media is such an important element that people now want integrating into their websites as standard. I think that I will definitely be using this in the future as the code is so simple to locate for me now and I know exactly what to do with it. Thank you

Nov 04 2011 22:14

Your all tricks and hacks are jakkash but a screenshot or demo will be better if you provide. :)

Dec 09 2011 11:10

hello
very goog trick
but how placing that above the content? and how to set the size and the position?
it’s a bit “rough” as it; it will be goog to tun the code a little bit in order to get a nice result.
best regards

Jan 27 2012 21:36

I paste this code into the functions.php file of my blog theme but it displays error message. Please tell me what to do.

Jul 17 2012 11:44

Hello,
for me it is the same, I paste this peace of code exactly so in functions.php. I always get errors – headers already sent by (output started at… and so on. What can I do?

Aug 15 2012 20:25

@Frank: Try removing all empty spaces between the

@Sam: You can place the share buttons above the post by removing the . infront of content. Check here for details: http://www.wpspells.com/add-fb-twitter-gplus-share-buttons/

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox