How to Add Retweet Text with Tinyurl in WordPress Posts


Ever since we posted our WordPress guide to Twitter Anywhere Platform, we have been getting requests about how to automatically generate post tweet text in the live tweet box. In this article, we will show you how you can add tweet text with Tinyurl in WordPress posts. We will utilize the tinyurl API to generate short url for your post and display it with the text.

First open your theme’s functions.php file and paste the following codes:

  function getTinyUrl($url) {  $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url);  return $tinyurl;  }

This function will allow us to generate tinyurls for your WordPress posts.

Then open your site’s footer.php and add the following code right above the closing body tag, OR you can add it in your site’s header.php before the closing head tag:

      <script src="https://platform.twitter.com/anywhere.js?id=YourAPIKey&v=1" type="text/javascript"></script>      <script type="text/javascript">      twttr.anywhere(onAnywhereLoad);      function onAnywhereLoad(twitter) {      // configure the @Anywhere environment      twitter("#custom-tweetbox").tweetBox({      label: "Retweet:",      defaultContent: "<?php      global $wp_query;      $postid = $wp_query->post->ID; ?>      Reading: <?php the_title(); ?> – <?php      $turl = getTinyUrl(get_permalink($post->ID));      echo $turl;      ?> (via @wpbeginner) ",      height: 50,      width: 480,      });      };      </script>

Make sure you add your API Key and change (via @wpbeginner) to your own twitter username. (If you want to know how to get a twitter API, then follow our twitter anywhere guide)

The final step is to open your single.php file and add the following code where you like:

  <div id="follow-wpbeginner"></div>

You would now have the live tweetbox in your post with a text displaying like:

Reading: Post Title – Tinyurl (via @wpbeginner)

If you don’t want to display the box in your post, then you can also try the plugin called Retweet Anywhere



Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.