Skip to content
  • Home
  • Shortcodes
  • Plugins
  • IT / Windows
  • Programming & Development
  • Random

12

Add Twitter Share Button to WordPress 3.0 with a simple shortcode

I recently created shortcodes for Google +1 and Facebook Like and Send button. Finally, to my favorite as I have taken to Twitter over the last year. I now have 500+ random tweets. If your theme doesn’t support Twitter this is a good way to extend its capability. Just remember that when you upgrade your theme you will need to re-add the code to your functions file.

The code was built with Twitter API documentation. To understand all the capabilities this shortcode supports you may what to head over and check out the types.

1) Open your Functions.php file – Create if does not exist in root of template

Assuming you have more than average knowledge and the ability to edit theme files directly in WordPress. This file serves as an entry point for your custom code in WordPress 3.0. So if you have custom classes or external code you need to run within the WordPress context you will add the hooks and init() code blocks there. The file location is ( site/wp-content/themes/YOUR-Theme/functions.php ).

2) Add the Twitter Share Shortcode to WordPress

Highlight the code box below and copy / paste in the functions.php file.

function twitter( $atts, $content=null ){
/* Author: Nicholas P. Iler
 * URL: http://www.ilertech.com/2011/07/add-twitter-share-button-to-wordpress-3-0-with-a-simple-shortcode/
 */
	extract(shortcode_atts(array(
        'url' => null,
		'counturl' => null,
		'via' => '',
		'text' => '',
		'related' => '',
		'countbox' => 'none', // none, horizontal, vertical
	), $atts));
	// Check for count url and set to $url if not provided
	if($counturl == null) $counturl = $url;
	$twitter_code = <<<HTML
	<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
<a href="http://twitter.com/share" class="twitter-share-button"
	data-url="$url"
	data-counturl="$counturl"
	data-via="$via"
	data-text="$text"
	data-related="$related"
	data-count="$countbox"></a>
HTML;
	return $twitter_code;
}
add_shortcode('t', 'twitter');

3) Add to Template Page with Shortcode

This is the most versatile way of adding the final piece of code. The shortcode.

Example 1

Fastest – Use for Single Pages auto detects page title and URL. Note: If title is too long tweet box will not work. Goto example 3 for trim function.

<!-- Twitter Share Shortcode -->
<?php echo do_shortcode("[t]"); ?>

Example 2

Slightly more overhead if you want to use in on the front page while inside the WordPress loop:

<!-- Twitter Share Shortcode - Get current link in loop -->
<?php echo do_shortcode("[t url='get_permalink();']"); ?>

Example 3

This is a more solid function that will get the WordPress URL and trim the title to 115 characters. It also works on the front page.

I have some pretty long titles here at ilertech.com, so a few of my share boxes required people to remove some text in order to submit to Twitter. You can change that to whatever you like.

Put this into your functions.php, than call it with “twitter_share()” in your template files.

<?php
function twitter_share(){ // (Must be called while in the loop)
/* Author: Nicholas P. Iler
 * URL: http://www.ilertech.com/2011/07/add-twitter-share-button-to-wordpress-3-0-with-a-simple-shortcode/
 */
	// Get the title and url of posts and pages
	$title 	= get_the_title();
	$url 	= get_permalink();
	// Shorten title if its too long
	if(strlen($title) < 115){
		$shortenedTitle = substr($title, 0, 115);
		$shortenedTitle .= '...';
	}
	echo do_shortcode("[t url='$url' text='$shortenedTitle']"); // Shortcode
}
?>

Call it from within the WordPress loop,

<?php twitter_share(); ?>

Shortcode Usage Examples

[t]

No settings.

[t related='nickiler: PHP Programmer and WordPress Hacker, ilertheme: Premium WordPress Themes']

This version displays @nickiler and @ilertheme as related accounts with description after the tweet to allow tweeters to follow your other accounts. Check screenshot:

Note: Adding a ‘via’ perimeter automatically displays the account and recommends the tweeter follow you.

[t countbox='horizontal']
[t countbox='vertical']

Related Posts

  • Add Twitter Follow Button to WordPress 3.0 with a simple shortcode
  • Add Facebook Like Button to WordPress 3.0 with a simple shortcode
  • Add Google + 1 to WordPress 3.0 with a simple shortcode
  • Escape WordPress shortcodes in content with a shortcode
  • What’s wrong with the WordPress Redirection Plugin?

About Nickiler

Web Developer and Systems Admin
View all posts by Nickiler →

Follow @Nickiler
Posted on July 3, 2011 by Nickiler
This entry was posted in Shortcodes and tagged twitter, wp. Bookmark the permalink.

12 Responses to Add Twitter Share Button to WordPress 3.0 with a simple shortcode

  1. Friiitz says:
    July 30, 2011 at 6:05 AM

    In #7 – the tweet button – there must be an error somewhere in this part of the code because I get a “Parse error: syntax error, unexpected T_SL” message in that line

    $twitter_code = <<<script src="http://platform.twitter.com/widgets.js" type="text/javascript"><!--mce:0--></script>
    <a class="twitter-share-button" href="http://twitter.com/share"></a>
    HTML;

    return $twitter_code;

    }
    I googled for that parse error message and, apparently, this is a whitespace problem in the <<<script…-part of the code. But I could not fix it. Any ideas?

    Reply
    • Nicholas P. Iler says:
      July 30, 2011 at 5:22 PM

      Hey Friiitz, Thanks allot for catching this! Are you by chance using Chrome or Safari?

      Possibly, recent updates to WordPress Core have thrown the code example out of whack. Some strange characters were added, and not 100% sure how or why. I also noticed while testing that double-clicking the code while using Chrome or Safari adds bad markup to all my examples, and will not work. If you instead just highlight the code in Chrome its seems to work just fine.

      I updated the code sample to remove the bad characters and now its fine… Again thanks for the catch !-)

      Reply
  2. Tomas says:
    October 15, 2011 at 12:30 PM

    Man you’re awesome. Best of luck in all u do. And keep it up 2.

    Reply
    • Nicholas P. Iler says:
      December 18, 2011 at 9:24 AM

      Thanks Tomas, Appreciation like this makes me wanna add more articles.

      Reply
  3. Nick says:
    December 15, 2011 at 12:40 PM

    I can’t seem to be able to call this in my loop — I’m really trying to use the vertical box example — do i put the shortcode or the <?php in the loop — I've tried both and nothing seems to work!

    N

    Reply
    • Nickiler says:
      December 18, 2011 at 9:38 AM

      As long as you add the shortcode to your functions.php file wrapped in < ?php CODE ?>, it should respond to the function call.

      If you want to add the shortcodes directly to your templates, which is the easiest you need to use the do_shortcode() function. I would highly recommend using the third example and just call the function without do_shortcode().

      This code will only work correctly inside the loop. (last I tested)

      <?php twitter_share(); ?>
      
      Reply
  4. Bob Axford says:
    May 6, 2012 at 9:17 PM

    Thanks very much for this, I’ve been battling with plugins to get them to work but this was so much easier!

    Reply
  5. Liz says:
    July 10, 2012 at 3:11 PM

    Hey! I’m also having the same issue as Friitz. Is there any chance that you might check on these now with 3.4 released? I’ve opened other browsers and tried to make the code work, but regardless of the source, I get errors around lines with: <<<<HTML and HTML;

    Reply
    • Nickiler says:
      July 10, 2012 at 6:47 PM

      I just upgraded the site to the latest version of WordPress and everything looks fine. I think the editor I’m using on the site has some bugs. Here is replacement code without the HERE statement:

      	$twitter_code =
      			"<script src='http://platform.twitter.com/widgets.js' type='text/javascript'></script>
      			<a href='http://twitter.com/share' class='twitter-share-button'
      			data-url='$url'
      			data-counturl='$counturl'
      			data-via='$via'
      			data-text='$text'
      			data-related='$related'
      			data-count='$countbox'></a>";
      
      Reply
      • Liz says:
        July 12, 2012 at 6:27 AM

        Awesome! This totally fixed the problem! Thank you thank you thank you!

        For anyone else who just wants to copy and paste, make sure to remove the errant at the very end, it looks like that might be part of the bug issue.

        Reply
        • Nickiler says:
          July 12, 2012 at 7:53 AM

          Great! I’m glad it worked for you. I also removed the random “code” tag that was at the end ;)

          Reply
    • Nickiler says:
      July 10, 2012 at 4:45 PM

      Hi Liz, I’ll update the site tonight to 3.4.1 and test it. Nick

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Recent Posts

    • WP Vanilla Connect
    • Add Twitter Follow Button to WordPress 3.0 with a simple shortcode
    • Add Twitter Share Button to WordPress 3.0 with a simple shortcode
    • Add Facebook Like Button to WordPress 3.0 with a simple shortcode
    • Escape WordPress shortcodes in content with a shortcode
  • Recent Comments

    • Scott Waidelich on [Updated] These files can’t be opened. Your Internet security settings prevented one or more files from being opened – Solved
    • Kenneth Deaville on [Updated] These files can’t be opened. Your Internet security settings prevented one or more files from being opened – Solved
    • Dimo on [Updated] These files can’t be opened. Your Internet security settings prevented one or more files from being opened – Solved
    • casino on Creating a USB bootable disk with a Custom Windows PE image using AIK
    • orthodontists in Middletown on Creating a USB bootable disk with a Custom Windows PE image using AIK
  • Categories

    • IT / Windows
    • Plugins
    • Programming & Development
    • Random
    • Shortcodes
    • WordPress
  • Archives

    • January 2012
    • July 2011
    • June 2011
    • September 2010
    • August 2010
    • June 2010
    • May 2010
    • November 2009
ilertech
Proudly powered by WordPress.