Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Add Retweet Text with Tinyurl in WordPress Posts

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
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="http://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

Source

Ruhanirabin


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. PicSeshu says:

    I know Andrew figured this out for Bit.ly but could you expand on how I would use that service instead of TinyURL? Thanks!

  2. Andrew says:

    This is great. How much harder would it be to implement this with bit.ly (or j.mp) instead of tinyurl?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.