“Tweet This!” short links for posts
I like to include an easy way for readers to “tweet” my posts, but the URLs are generally way over Twitter’s 140-character limit. Fortunately, we can use PHP’s handyfile_get_contents
function to grab short versions of our URLs from a free minifying service like TinyURL.All we need to do is add the following script to your web pages or your
functions.php
file:Then, in the location where you would like to display your shortened “Tweet This!” links (e.g., after your post loop), call the function and create the link with the following code:
This is basically a link using the Twitter API to specify the contents of our tweet. For each post, the value of
the_title()
and get_permalink()
will change to reflect its title and URL, respectively. If you are using a platform other than WordPress, you will need to use the equivalent of these dynamic tags.Once you have this code in place, it will generate “Tweet This!” links that send the following information to Twitter (using this post as an example):
As you can see, the original long URL for this post was automatically shortened via the TinyURL service. Nice, clean and effective.
For more social media links, check out my comprehensive article, Fully Valid, SEO-Friendly Social Media Links for WordPress.
Custom short links using your own domain name
If you would rather not rely on a shortening service (such as TinyURL) for your shortened links, fret not — it is easy to create your own, customized URLs. There are several good reasons why you might prefer to use your own domain for your shortened URLs, including:- preservation of valuable link juice
- prevention of link loss if the shortening service dies
- control over the appearance and branding of your links
- http://perishablepress.com/press/?p=711
- http://perishablepress.com/press/?p=712
- http://perishablepress.com/press/?p=713
.
htaccess
file as our WordPress permalinks:This directive will redirect our custom shortened URLs to our default WordPress URLs, which WordPress will then redirect according to any existing permalink rules. Once this technique has been implemented, the following redirects will take place for each of your custom URLs:
Once we have the HTAccess code in place, we use the same basic markup as in our previous example to create the link. Add the following code to the desired location in your theme file (e.g., after the post loop):
This code will create “Tweet This!” links that send the following information to Twitter (using this post as an example):
As you can see, the original long URL for this post is now a customized short URL using the same domain. Feel free to change the “
x
” to whatever you want — just remember to edit both the markup link and the .
htaccess
directive if you decide to do so.I should also note that, for any of these “shortened” URL examples, we are only using Twitter as an example application. Many sites these days simply include a shortened version of the post URL for people to copy & paste anywhere they wish. To do something similar with the current technique, we would simply include the following snippet:
<?php echo get_option('home'); ?>/x/<?php the_ID(); ?>
Drop-dead simple short URLs using the post ID
For the sake of completeness, keep in mind that we can use the post ID to (re)create our original WordPress URL structure:http://domain.tld/?p=719
That’s pretty short, and may work perfectly for your short links if the format is suitable for you. All we would need to do is include the following code into your WordPress theme:
<?php echo get_bloginfo('url')."/?p=".$post->ID; ?>
WordPress will automatically redirect such URLs to the corresponding permalink (if you have permalinks enabled).
Display the number of your Twitter followers in plain text via JavaScript
Another fun thing to do with Twitter is show off your subscriber number on your website. Many sites these days prominently display two numbers: one for their Feedburner subscriber count and another for their Twitter follower count. Displaying either count with a button or badge is a no-brainer, but displaying the numbers in plain text allows for greater stylistic control. Plain text is desirable because you can do just about anything with it, whereas a badge just kind of sits there, looking ugly.To get our Twitter follower count in plain text, we need to include some free JavaScript made available to us at TwitterCounter. Just place this code wherever you would like to display your follower count:
Simply edit the “
username
” to match your own and you are off to the races.Display the number of your Twitter followers in plain text via PHP
If you would rather not rely on JavaScript and a third-party service to deliver your follower count data, we can use PHP’s excellentcurl()
functionality (edit the “www.domain.tld
” to match your own):That curl snippet will grab the content of the input URL and prepare it for parsing via the following code:
This second snippet uses SimpleXML to parse the curl output via the Twitter API. SimpleXML is pretty common on most servers running at least PHP 5. The only thing you need to edit is the “
username
” in the last line. Everything else is roses. Drop this code into a web page and enjoy your follower count displayed anywhere you like, and in plain-text, no less.Display the number of tweets for each page or post
If you are running SimpleXML (you probably are, ask your host), displaying the total number of tweets for any given post or page is easily accomplished with the following slice of PHP:If you are using WordPress, you may place that code into your active theme’s
functions.php
file. Then, to display the total number of tweets for your posts, you would add the following function call to your post loop:<?php tweetCount($post->permalink); ?>
You can also show the number of tweets for a particular page, even if it is not on your site. For example:
<?php tweetCount("http://perishable.biz"); ?>
Backup your tweets
Once you have been tweeting for awhile, you may begin to wonder what would happen if Twitter ever lost all of your tweets. It could happen, and if it does, do your tweets contain anything of value? If they do, I would highly recommend making regular backups (like monthly or something) of all your tweets. Doing so is easy. All you need to do is login to your Twitter account and enter the following URL into your browser:Before this will work, you need to replace the “
username
” with your actual username, and also change the “n
” with the number of tweets you would like to download. To download all of your tweets, check your number of updates on your Twitter profile page. After entering this URL into your browser, you will be taken to an XML page containing your tweets. To make a backup, just save that page to a safe location on your computer.Now you can rest so easy just knowing deep down that all your precious tweets are safe and sound ;)
0 comments:
Post a Comment