Family Encyclopedia >> Electronics

How to Add Custom Retweet Text with TinyURL Links in WordPress Posts

As experienced WordPress developers, we've fielded many requests since our Twitter Anywhere guide on automatically populating tweet text in live boxes. This proven tutorial shows you how to integrate custom tweet text with TinyURL short links in your posts using the TinyURL API.

First, open your active theme's functions.php file and add this reliable function:

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

This function generates TinyURLs for any WordPress post URL seamlessly.

Next, edit your site's footer.php and insert the following just before the closing </body> tag. Or add it to header.php before </head>:

<script>
twttr.anywhere(function(twitter) {
    twitter("#custom-tweetbox").tweetBox({
        label: "Retweet:",
        defaultContent: "Reading: <?php echo get_the_title(); ?> - <?php echo getTinyUrl(get_permalink()); ?> (via @yourusername)",
        height: 50,
        width: 480
    });
});
</script>

Replace @yourusername with your Twitter handle. For API setup details, see our Twitter Anywhere guide.

Finally, open your single.php template and add this where you want the tweet box:

<div id="custom-tweetbox"></div>

Your posts will now feature a live tweet box pre-filled with: "Reading: [Post Title] - [TinyURL] (via @yourusername)".

For a no-code option, install the Retweet Anywhere plugin.

Source

Ruhanirabin