TinyURL integration with your blog

TinyURL and WordPress Intergration

Make it easier for your readers to share the URL of your posts by integrating the TinyURL service into your blog. A demonstration of the final product can be seen by scrolling to the bottom of this page. In order to follow this tutorial successfully,  access to PHP will be needed. Additionally, we will be using the TinyURL API so make sure your content follows their Terms of Service agreement. The guide below is made for Self-hosted WordPress blogs. However, the code is simple enough to be modified and used on any other platform.

Functions.php

Placing a piece of code into this file allows it to be called from anywhere else in the entire theme. With this in knowledge, we will create a function by the name of MakeTinyURL and insert it into this file. MakeTinyURL will be responsible to communicating with the TinyURL api and generating the shortened addresses for our posts.

 function MakeTinyURL($p){
     echo file_get_contents('http://tinyurl.com/api-create.php?url='.$p);
  }

Implementing the Code

To use the code, simply call the MakeTinyURL function we just created and input the full path to the post in place of the variable $p. In WordPress, we can automate TinyURL generation through the use of the built-in get_permalink() function. The get_permalink() function will automatically feed the newly created MakeTinyURL() the complete path to our post. To automatically generate a shortened URL and output it, insert the code below within the pages.php or index.php (on some templates).

< ?php MakeTinyUrl(get_permalink()); ?>

This is all it takes to fully implement TinyURL into your blog. If you found this post useful, feel free to leave a comment! We would also appreciate it if you share valuable content via Delicious and other social platform!

Related Posts Plugin for WordPress, Blogger...