** this article has been updated 23rd August 2014, to work with Twitter [dtwd-tweet text="updated article on how to add Retweet button shortcode" hashtags="WordPress, Tweet button"]Adding a Retweet button onto a WordPress page is really quite simple. There are many plugins that you could use. Alternatively you could simply add some javascript from a social sharing site straight onto your pages.Some Wordpress themes may already have a shortcode that you can use, however if not, here is another way that you could add Retweet Buttons to your page.Firstly go grab some code over at the official Twitter Buttons page.

add retweet button shortcode

Once you have selected your preferences, copy your code. It will look something like this:[php]<a href="https://twitter.com/share" class="twitter-share-button" data-text="Reading this article" data-via="MrDavidTiong" data-size="large" data-hashtags="WordPress">Tweet</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>[/php]The first part that is wrapped up in your <a href=...>Tweet</a> is your link code, and the second part is your javascript.Now we will create a simple shortcode so that whenever you want to use the Retweet button you can simply type in something short like [dtwd_tweet]. The other advantage of using a shortcode is that if you decide to change some of those original preferences, you just modify your shortcode in the functions.php file of your childtheme, and the changes will be reflected wherever that shortcode has been used.First step to creating the shortcode is to setup your function, which we will call dtwd_tweet_this. We will add the script at the start, followed by the link:[php]function dtwd_tweet_this() { ?>ADD YOUR SCRIPT HERE<?php return 'ADD YOUR LINK HERE'; }[/php]So it should look something like mine below:[php]function dtwd_tweet_this() { ?><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script><?phpreturn '<a href="https://twitter.com/share" class="twitter-share-button" data-text="Reading this article" data-via="MrDavidTiong" data-size="large" data-hashtags="WordPress">Tweet</a>'; }[/php]***The update ***Now this was working, however recently I noticed that it stopped functioning, so I'm going to suggest modifying your above link in the following way:[php]return '<a href="https://twitter.com/share?text=Reading this article&via=mrDavidTiong&hashtags=WordPress" class="twitter-share-button" data-size="large" data-lang="en" >Tweet</a>';[/php]Obviously replace "Reading this article" with your preferred text, "mrDavidTiong" with your Twitter handle, and "WordPress" with your preferred hashtag(s).Now the next step is to tell WordPress that you want a shortcode called [dtwd_tweet] that activates the dtwd_tweet_this function.[php]add_shortcode('dtwd_tweet', 'dtwd_tweet_this');[/php]so the complete code that goes into your childtheme's functions.php file will be something like this:[php]function dtwd_tweet_this() { ?><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script><?phpreturn '<a href="https://twitter.com/share" class="twitter-share-button" data-text="Reading this article" data-via="MrDavidTiong" data-size="large" data-hashtags="WordPress">Tweet</a>'; }add_shortcode('dtwd_tweet', 'dtwd_tweet_this');[/php]and to use your new shortcode you simply type in [dtwd_tweet] into your posts and pages where you want it to appear and it will look like this ---> [dtwd-tweet text="Reading this Article" hashtags="WordPress, Retweet Buttons"]
Premium Club Members can access additional code options below:Viewing Premium Content - thanks for being a Premium Club Member
Additional Shortcode options to include fields that you can modify when using the retweet button:[php]function dtwd_tweet_this($atts) { extract(shortcode_atts(array( 'text' => 'read this article', 'hashtags' => 'WordPress', ), $atts)); ?><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script><?php return '<a href="https://twitter.com/share?text='.$text.'&via=mrDavidTiong&hashtags='.$hashtags.'" class="twitter-share-button" data-size="large" data-lang="en">Tweet This</a>'; }add_shortcode('dtwd_tweet', 'dtwd_tweet_this');[/php]
Again, be sure to replace "mrDavidTiong" with your Twitter handle, and change the defaults 'read this article' and 'WordPress' to be applicable for your website or blog.Now when you use the shortcode you can specify different hashtags and different text for each article and for different sections of an article.For eg, I could use different buttons on my pages like this:[dtwd_tweet text="check out this article I am reading" hashtags="Twitter, WordPress"]then in a different section of my article I could use:[dtwd_tweet text="this tip just made my day" hashtags="Retweeting"]etc.
[dtwd-related-posts-sc title="Perhaps some more reading ..." ]

Comments: