Adding Social Media Links to WordPress Without Using a Plugin
There are tons of WordPress plug-ins that offer you the ability to share your blog posts using social media websites. Many of these plug-ins word great (bookmarkify did its job for us in the past) but you are limited to the websites and, most of the time, the design. You also may be limited to the message that you want to include with the link – for example, adding your @Twitter ID to the Twitter link – and not being able to include URL shortening.
So what if you want a completely custom way to share your links? Keep reading, I’ll share the code that I used to convert the Local Wisdom blog from being plug-in dependent to having our own custom code.
Part 1: The Code
In order to pick up the URL and title of the blog post, whether it be on an index, archive or single page, use the codes below. If you’ve been playing with your WordPress theme code, these links should look very familiar.
Permalink to post:
<?php the_permalink(); ?>
Title to Post:
<?php the_title(); ?>
Part 2: Inserting the Code into the Correct URLs
Now that you have the code that will pick up the title and URL of a single post, you need a way to actually link them into the social networking sites. Below is a list of commonly used social bookmarking sites:
Email with Feedburner
http://www.feedburner.com/fb/a/emailFlare?itemTitle=<?php the_title(); ?>&uri=<?php the_permalink(); ?>&loc=$loc
Bloglines
http://www.bloglines.com/sub/<?php the_permalink(); ?>
BlogMarks
http://blogmarks.net/my/new.php?mini=1&simple=1&url=<?php the_permalink(); ?>&title=<?php the_title(); ?>Blinklist
http://blinklist.com/index.php?Action=Blink/addblink.php&Url=<?php the_permalink(); ?>&Title=<?php the_title(); ?>
Del.icio.us
http://del.icio.us/post?url=<?php the_permalink(); ?>&title=<?php the_title(); ?>
Digg
http://digg.com/submit?phase=2&url=<?php the_permalink(); ?>&title=<?php the_title(); ?>
diigo
http://www.diigo.com/post?url=<?php the_permalink(); ?>&title=<?php the_title(); ?>
dzone
http://www.dzone.com/links/add.html?description=<?php the_title(); ?>&url=<?php the_permalink(); ?>&title=<?php the_title(); ?>
http://www.facebook.com/share.php?u=<?php the_permalink(); ?>
Google Bookmarks
http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=<?php the_permalink(); ?>&title=<?php the_title(); ?>
Kaboodle
http://www.kaboodle.com/za/selectpage?p_pop=false&pa=url&u=<?php the_permalink(); ?>
http://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink(); ?>&title=<?php the_title(); ?>
Mixx
http://www.mixx.com/submit?page_url=<?php the_permalink(); ?>
MySpace
http://www.myspace.com/Modules/PostTo/Pages/?c=<?php the_permalink(); ?>&t=<?php the_title(); ?>
Newsvine
http://www.newsvine.com/_tools/seed?popoff=0&u=<?php the_permalink(); ?>
Propeller
http://www.propeller.com/submit/?U=<?php the_permalink(); ?>&T=<?php the_title(); ?>
http://reddit.com/submit?url=<?php the_permalink(); ?>&title=<?php the_title(); ?>
Shoutwire
http://www.shoutwire.com/?p=submit&link=<?php the_permalink(); ?>
Squidoo
http://www.squidoo.com/lensmaster/bookmark?<?php the_permalink(); ?>
Slashdot
http://slashdot.org/bookmark.pl?url=<?php the_permalink(); ?>&title=<?php the_title(); ?>
StumbleUpon
http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&title=<?php the_title(); ?>
Technorati
http://technorati.com/faves?add=<?php the_permalink(); ?>
http://twitter.com/home/?status=<?php the_title(); ?>+<?php the_permalink(); ?>
Windows Live
https://favorites.live.com/quickadd.aspx?mkt=en-us&url=<?php the_permalink(); ?>&title=<?php the_title(); ?>
Yahoo! Bookmarks
http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=<?php the_permalink(); ?>&t=<?php the_title(); ?>
Part 3: Automatically Shorten the URL
On sites like Twitter, you may want to consider adding code that automatically shortens the URL since you have a character limit within the posts. For Local Wisdom, I am using code that I found over at mattflies.com that automatically creates bit.ly URLs. Here's a quick overview of it:
First, be sure you have an account with bit.ly. You will need to insert both your user name and API key into the code. You can find your API key on your account settings page.
Add the following code to your functions.php file. Remember to back this file up before playing with it just in case you have to revert back.
/* bit.ly urURL */
function getBitlyUrl($url) {
$bitlylogin = 'YOUR LOGIN NAME';
$bitlyapikey= 'YOUR API KEY';
$bitlyurl = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".$bitlylogin."&apiKey=".$bitlyapikey);
$bitlycontent = json_decode($bitlyurl,true);
$bitlyerror = $bitlycontent["errorCode"];
if ($bitlyerror == 0) {
$bitlyurl = $bitlycontent["results"][$url]["shortUrl"];
}
else $bitlyurl = "error";
return $bitlyurl;
}
Don't forget to change this part with your bit.ly username and API key:
$bitlylogin = 'YOUR LOGIN NAME'; $bitlyapikey= 'YOUR API KEY';
In order to use the short URL conversion for your link, you will replace the <?php the_permalink(); ?> code inside your link with the following:
<?php $burl = getBitlyUrl(get_permalink()); echo $burl; ?>
Note: you can change <$burl> to anything you please. If you would like to read more about the URL shortening, refer back to mattflies.com's blog entry.
Part 4: Our Code - an Example
To see a working example, here is the code that I used for Local Wisdom's social sharing links as they are now (minus our formatting). Notice the custom message and bit.ly URL in the Twitter link.
<ul> <li><a href="http://del.icio.us/post?url=<?php echo get_permalink() ?>&title=<?php the_title(); ?>">del.icio.us</a></li> <li><a href="http://www.facebook.com/share.php?u=<?php echo get_permalink() ?>">Facebook</a></li> <li><a href="http://digg.com/submit?phase=2&url=<?php echo get_permalink() ?>&title=<?php the_title(); ?>">Digg</a></li> <li><a href="http://twitter.com/home/?status=<?php the_title(); ?> - <?php $burl = getBitlyUrl(get_permalink()); echo $burl; ?> (via @localwisdom)">Twitter</a></li> <li><a href="http://www.feedburner.com/fb/a/emailFlare?itemTitle=<?php the_title(); ?>&uri=<?php echo get_permalink() ?>&loc=$loc">E-mail</a></li> </ul>
Place this code anywhere within the have_posts() loop and you're good to go! (added 4/5/10)
Find this article helpful? Feel free to share it using our social bookmarking links!