wordpress Tag Archive

Custom Shortlinks for WordPress

I was lucky and managed to get my hands on a neat little .co domain, djr.co, and I decided that since it is now the shortest domain that I own that I’d first put it to use for the short links on my blog, and as my own personal url shortener.

I was lucky and managed to get my hands on a neat little .co domain, djr.co, and I decided that since it is now the shortest domain that I own that I’d first put it to use for the short links on my blog, and as my own personal url shortener.

The (really) short version

  • I’ve set up Lessn to take care of the personal url shortener side of things
  • And I’m using a simple combination of a WordPress filter function and an .htaccess/index.php redirection combo to handle the shortlinks for my blog.

The long version (for those who want more info)

Personal URL shortener

For the general url shortener there a few different options floating around, but I didn’t need anything super fancy with graphs and counters and all that stuff, I just wanted something simple and easy to setup — and most importantly something that worked. So I went with Shaun Inman’s nice little Lessn script and its working great.

Summary — if you want to set up your own url shortener using your own domain, then give Lessn a go. It is dead simple to setup.

Custom WordPress ‘shortlinks’

As of version 3.0, WordPress now come with a built in function called ‘wp_get_shortlink’ which, as you can probably guess, returns a ‘shortlink’ for the relevant post/page.

By default these ‘shortlinks’ take the form of http://deanjrobinson.com/?p=1345 — which also happens to be the default post links if you don’t have pretty permalinks set up, I’ll explain why this point is important shortly.

Getting djr.co ready for redirection

The first thing I needed to do was to set up things on djr.co and make sure it was working as I expected.

I had considered using the api built into Lessn to auto generate a shortlink when I publish a post, I even found a plugin that would have done it for me pretty easily, however I rules this out for a couple of reasons:

  • It would have meant adding additional meta data fields against posts — not necessarily a bad thing, but also not really necessary for my needs
  • I would have had to either modify my templates to output the new short links, or create a filter to take care of it for me
  • And the main reason was that I wanted ‘shortlinks’ to be available for all my old posts as well — without me having to go back and generate them.

The ‘solution’ I decided on is essentially a really stripped back version of Lessn — without the need to call off to the Lessn database etc because our WP posts already have unique ids which we can use for the redirection. More specifically it just two files a .htaccess file and an index.php file.

First, the .htaccess file, this is exactly the same as the one from Lessn.

This takes the url, eg. http://djr.co/m/1345, and passes it to
http://djr.co/m/index.php?token=1345 ready for the redirection.

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond	%{REQUEST_FILENAME}	!-d
  RewriteCond	%{REQUEST_FILENAME}	!-f
  RewriteRule	(.*) index.php?token=$1	[QSA,L]
</IfModule>

You might be curious as to why I haven’t just redirected it to my primary url using the htaccess, I’ll get to that in a sec.

Second, the simple index.php

This is based on the index.php file from Lessn, but cut down to just what I needed for my blog redirection. That means no need to call any of the Lessn includes, or make any database queries, just grab the id and redirect it off to my blog.

If no ‘token’ is passed though the page will simply return a 404. The two reasons for using this index.php file to handle the actual redirect are so that I can clean up the token (using strip_tags) just in case someone tries something nasty, and so that I can check that the token is numeric (using is_numeric) — this is because WordPress post/page IDs are only numeric, so any other values obviously aren’t going to work anyway.

This is what the cut-down, modified index.php file looks like. You’ll notice that the url format for the ‘Header Location’ matches the default WordPress ‘shortlink’ format I mentioned above. This is because no matter how you have your pretty permalinks set up, these default links will always work.

<?php

if (!empty($_GET['token'])) {
  $id = strip_tags($_GET['token']);
  if(is_numeric($id)) {
    header($_SERVER['SERVER_PROTOCOL'].' 301 Moved Permanently');
    header('Location:http://deanjrobinson.com/?p='.$id);
    exit();
  }
}

header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
header('Status:404');
die('404 Not Found');

?>

Getting WordPress to use these custom shortlinks

To get this working, all I needed to do was add a really simple ‘filter’ to the functions.php file in my current theme.

I’m using a filter so that I can continue to use the built-in ‘wp_get_shortlink’ function that is available in WordPress (since 3.0). This function can be used something like this:

<a href="<?php echo wp_get_shortlink(); ?>" title="ShortURL for this post">Short URL</a>

By running the filter over the standard function not only do I not have to update any of my theme templates, I will also be able to retrieve my custom short url by using the “Get Shortlink” button that has been added to the create/edit post screen in the WordPress admin, just below the post title.

<?php

function my_custom_shortlink_filter() {
  global $post;
  return 'http://djr.co/m/'.$post->ID;
}
add_filter('get_shortlink','my_custom_shortlink_filter');

?>

Best of all, because the shortlinks are based on the unique post ids (which every post has) the shortlinks will instantly work for all my posts going right back to the beginning of this blog without me needing to change anything else. Yay.

Article 23 Jul 2010 1 comment
·•·

Classic comments. There are some highly entertaining comments in this post (if you’re following the #thesiswp ‘debate’). By the reasoning of @pearsonified the 27,000 users of Thesis make him “one of the top 3 most important figures in the history WordPress”, so do the 83,000+ users of Redoable (plus however many thousands use it on WordPress.com) make me one of the top two in WP history. Of course it doesn’t. Just one of the many stupid arguments he’s put forward in the last couple of days.

Link 16 Jul 2010 0 comments
·•·

Fluency 2.3.2 — bug fixes and translations

Ok, it has taken a couple of weeks and about a dozen different WP installations, but I finally managed to replicate the mysterious “disappearing ‘hide menu’ button” bug that so many of you were reporting — so hopefully this update should resolve that issue (it resolved the occurrence that I produced). Also included are the first batch of translations (Italian, Turkish, Spanish and Chinese). As always, grab the update from here or use the built-in auto-updater.

·•·

Fluency 2.3.1 update now available

This minor update should hopefully fix the two common issues people were experiencing with the initial release of 2.3 — the menu overlap problem on the ‘Menu’ management page, and the lack of two-column hover menus. There are a couple of other small tweaks in there too, plus a new ‘coffee’ colour scheme. Grab the update from here or just use the auto-updater that’s built into WordPress.

·•·

Fluency Admin 2.3 now available

Now compatible with WordPress 3.0. I’ve also added more customisation options including iPad friendly menus, and the ability to turn on/off hover menus and hot keys as needed.

Fluency Admin 2.3 now available

Fluency 2.3 is a recommended upgrade for all Fluency users who’ve already upgraded to WordPress 3.0 (which was released earlier this week). Included in this update, in addition to WP3.0 support, are fixes for a number of small issues that were pointed out by users over the past 6 months, and a few additional options to ‘fine-tune’ how you use Fluency.

Well, what are you waiting for? Read more about the updates here, or head to your admin panel to upgrade/install Fleucny 2.3 (of course, you can also jump straight over to WordPress.org to grab the download from there too.)

As always, if you spot a bug or something I’ve missed please let me know. The best place for you to do so is in my help forums, or, if you prefer, in the WordPress.org support forums.

·•·

Enabling Twitter avatars within BuddyPress

First up, the first step in this process is to install the Twit Connect plugin which you can do by following these instructions, then you can proceed with enabling Twitter avatars.

First up, the first step in this process is to install the Twit Connect plugin which you can do by following these instructions, then you can proceed with enabling Twitter avatars.

This is just a quick filter that I wrote while I was setting up help.deanjrobinson.com last week after I discovered that the twitter avatars weren’t working because of a different function/filter being used by BuddyPress.

I make no claims that this additional function is perfect, but I haven’t come across any problems with it yet, and I only knocked it together quickly. If you find anything wrong with it, or have any suggestions as to how it could be improved/simplified please let me know.

This new function based on one of the original functions that comes with the Twit Connect plugin which enables twitter avatars on regular WordPress blogs, just a with a few tweaks.

What you need to do is add the following code to the functions.php in your current BuddyPress theme. You can download a plain text copy of this function here (it’ll probably more reliable than copy-and-pasting from this page). Remember to backup your functions.php file before making any changes… just in case.

UPDATE: Appears some people who added this function the functions.php file in the default BuddyPress theme were seeing some error messages (I have not yet been able to recreate this error, but more than one user has reported it). Ideally you shouldn’t be modifying the default BP theme directly, you should be doing it via a child theme (find out how), so this code should be then added to the functions.php in your child theme.

7th March, UPDATE I’ve updated the function below (and the downloadable version above) to use http://tweetimag.es for the static twitter avatar URLs, it looks like it might a more reliable/consistent service.

<?php

 function bp_twc_get_avatar($avatar, $id_or_email='') {
  global $comment, $twc_user_login_suffix;

  if(is_object($comment)) {
   $user_id = $comment->user_id;
  }

  if (is_object($id_or_email)) {
   $user_id = $id_or_email->user_id;
  }

  if (is_array($id_or_email)) {
   if ($id_or_email['object']=='user') {
    $user_id = $id_or_email['item_id'];
   }
  }

  if (get_usermeta($user_id, 'twcid')) {
   $user_info = get_userdata($user_id);
   $twav_suffix = '';

   if ( $id_or_email['width'] ) {
    $twav_size = $id_or_email['width'];
    if( $twav_size < 32 ) {
     $twav_suffix = 'm';
    } else if ( $twav_suffix < 64 ) {
     $twav_suffix = 'n';
    } else {
     $twav_suffix = 'b';
    }
   } else if ( 'full' == $id_or_email['type'] ) {
    $twav_size = BP_AVATAR_FULL_WIDTH;
    $twav_suffix = 'b';
   } else if ( 'thumb' == $id_or_email['type'] ) {
    $twav_size = BP_AVATAR_THUMB_WIDTH;
    $twav_suffix = 'n';
   }

   $out = 'http://img.tweetimag.es/i/'. str_replace($twc_user_login_suffix,"",$user_info->user_login) . '_' .$twav_suffix;

   $avatar = "<img alt='Twitter Avatar' src='{$out}' class='avatar avatar-{$twav_size}' height='{$twav_size}' width='{$twav_size}' />";
   return $avatar;

  } else {
   return $avatar;
  }

 }

 // Check if Twit Connect exists (since its without it this function is pointless)
 if( function_exists( 'twit_connect' ) ) {

  add_filter( 'bp_core_fetch_avatar', 'bp_twc_get_avatar',10,4);

 }

?>

Show me the original version

I will add an explanation of what its doing if people are interested/curious.

·•·
·•·

New WordPress theme: Dear Diary

Dear Diary is a super simplistic theme for WordPress. It is best suited to a basic journal or diary where content is king and the theme should stay mostly out of your way.

Dear Diary is a super simplistic theme for WordPress. It is best suited to a basic journal or diary where content is king and the theme should stay mostly out of your way.

deardiaryheader

What’s it great for?

With its ruled-notepad styling Dear Diary is perfect for a simple online journal or diary. Of course you can still posts images and videos etc, but simple text is what Dear Diary does best. You can check out a full live preview here.

» Continue reading “New WordPress theme: Dear Diary”

Article 24 Oct 2009 2 comments
·•·

Fluency Admin 2.1.1 update available now After 2.1 hit 5,000 downloadsin less than a month I decided I’d knock out a quick update. Its not a huge update, but it does fix a few plugin display issues for the following plugins: Acronyms, NextGen Gallery, One-Click Plugin Updater, HeadSpace2 and WP-Polls. Fixes broken styling of Media Library popup and also added an option to set custom logo to display at the top of the menu (replaces WordPress logo). Download it here. Find out more here.

Link 18 Oct 2009 0 comments
·•·

Introducing WPAPI.ORG

Ok, so I actually launched this last weekend, but I’m only just now getting around to writing (briefly) about it. WPAPI.ORG is a super easy to use API which you can use to retrieve stats for all those great plugins and themes that you’ve got hosted on WordPress.org. Why would you want to do this?

Ok, so I actually launched this last weekend, but I’m only just now getting around to writing (briefly) about it. WPAPI.ORG is a super easy to use API which you can use to retrieve stats for all those great plugins and themes that you’ve got hosted on WordPress.org. Why would you want to do this?

WPapi.org

Why did I build this thing?

Because I could. No, seriously, the reason that I’ve built this (and made it available to everyone else) is that I was looking for ways to pull back the stats from WordPress.org and display them on the project pages here on my site, maybe even with some sexy graphs using something like Raphaël JS. After some investigation I found the xml feed that powers the graphs on WordPress.org, but that didn’t really help me. What I really wanted was JSON.

» Continue reading “Introducing WPAPI.ORG

Article 18 Oct 2009 0 comments
·•·
twitter was not updated. | lastfm was not updated. |