Select Page

So, I was using a third party plugin to handle all my social media sharing, which was very cool at first, but it started slowing my site down (as it connected to another site), so a few hours ago I ventured out onto the web trying to find the code to do this all myself.

The majority of what I used is copied and pasted from various sources, but also customised for WordPress and its functions. Speaking of functions, that is where you should head off to first.

Shortening your URLs

There is a wicked little URL shortening service that has an openly usable API which is very easy to implement. To edit functions, browse to your current theme’s folder, and edit the functions.php file.   At the end of the file, add the following lines:

function CompressURL($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “”.urlencode($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$shorturl = curl_exec ($ch);
curl_close ($ch);
return $shorturl;

This will allow you to share shortened URLs using the service.   This code was taken from the API documentation.

For the rest of the steps in this document, you will need to edit single.php.   I placed the code within a <div class=”SocialIcons”> [code for sharing] </div> block.


This was the easiest of the lot, which I got from trawling through the design of (thanks to battica for the code in there!).   Here is the code used for Facebook:

<a target=”_blank” title=”Facebook” href=”<?php $turl = CompressURL(get_permalink($post->ID)); echo $turl; ?>&t=<?php the_title(); ?>” rel=”nofollow” class=”social-bookmark”>
<img alt=”Facebook” src=”<?php bloginfo(‘template_directory’); ?>/images/facebook.jpg”/>

This should work off the bat (make sure you have an image ready in your theme’s images folder).


This one was a bit more complex, and took me a while because I had many many issues with the URL compression that was originally suggested in the site I used (cannot remember the site as I closed the tab by accident).   Turned out that it wasn’t as plug and play as the author had stated, and I had to play with this for a good hour or more…   Here is the Twitter code I ended up using, any variables that you would need to change for your blog are in bold italic.

<a target=”_blank” title=”Twitter” href=” <?php the_title(); ?> – <?php $turl = CompressURL(get_permalink($post->ID)); echo $turl; ?> (via @nightwulfe)” rel=”nofollow” class=”social-bookmark”>
<img alt=”Twitter” src=”<?php bloginfo(‘template_directory’); ?>/images/twitter.jpg”/>

Again, make sure you have the required images in your theme folder.   Replace the name after the ‘@’ with your Twitter account name.


This one was a difficult one, in fact, it still is.   Tonight was my first attempt at php.   Thankfully I have the equivalent of an SCJP, so I could read code and play with functions/methods etc, and be able to understand the WordPress function API.   Having said that, this one is still messing me about.   Don’t get me wrong, it does work.   However, I believe it could work better.   The code for this is based on the LinkedIn developer widgets (note that there is a license agreement involved with LinkedIn!).   Again, variables to change are in bold italic.

<a target=”_blank” title=”LinkedIn” href=”<?php the_permalink(); ?>&title=<?php the_title(); ?>&source=add_your_url_here” class=”social-bookmark”>
<img alt=”LinkedIn” src=”<?php bloginfo(‘template_directory’); ?>/images/linkedin.jpg”/>

Yet again, make sure your image is there…   Now, the thing that got me.   As stated with the LinkedIn widgets, there is a variable in the URL called &summary=.   In here, I was hoping to place my post excerpt from within WordPress for that particular post.   I tried various degrees of <?php wp_trim_excerpt(“<?php get_the_content(); ?>); ?>, just plain <?php get_the_content(); ?>, and a whole bunch of other methods I no longer care to remember, however all I could get generated in the output string was nothing.   It always stayed at ‘…&summary=&…’.   So, if you know how I can extract my excerpt from a post, please please, let me know in a comment!

RSS 2.0

Last but not least, I wanted my whole site to have a link to the RSS 2.0 feed.   I placed this in my header.php, but really, it is up to you where you want it.

<a title=”<?php _e(‘RSS 2.0 Feed’); ?>” href=”<?php bloginfo(‘rss2_url’); ?>”>
<img src=”<?php bloginfo(‘template_directory’); ?>/images/rss.jpg” alt=”RSS 2.0 Feed” />

And, yet again, make sure you have the image in the right place…

So that was my entire afternoon/evening, and I have such a feeling of accomplishment from it!

In hindsight, I could have done this by copy pasting the whole lot from the site I eventually found here at B2B Online Marketing, but as Murphy would have it, I would only find that site once I had finished everything else…

Note: If the code above does not work for you, please leave me a comment, I neatened it up in the article above here and may have wiped out a space or two in the process.