An example of this feature is shortening this post's URL: http://www.igorkromin.net/index.php/2015/02/07/add-a-url-shortener-plugin-to-flatpress/ to this: http://kr0m.in/6joXUt4. When the user goes to the short URL, they are automatically redirected to the long URL.
This post assumes that pretty URLs are being used already and that you have two domain names registered, the real, 'long' domain name, and the 'short' domain name. The examples I will use here are what I have set up for this blog, igorkromin.net and kr0m.in respectively.
Before you get started make sure your hosting provider sets up the short URL as an alias to your long URL in their DNS server otherwise this will not work.
The plugin consists of a handful of PHP files and a Smarty template for configuring options in the admin screen for flatpress. The basic idea with this plugin is to take the FlatPress internal post ID, which contains a unique 8 digit number, convert it to a short ID and use that for the short link. Then, when the short link is received, the short ID is converted back to the 8 digit ID, an the post link is looked up using FlatPress' get_permalink call.
Before going further, lets look at the structure and contents of the plugin. First there is the plugin directory, shorturl, which will be inside the fp-plugins directory. The shorturl directory structure is as follows:
The admin.plugin.shorturl.tpl file has the configuration template for the admin screen, the plugin.shorturl.php file has the plugin code to set up the config screen and to add a Smarty filter that generates the short URLs, the redirect.php file is used to redirect short URLs to the real, long URLs and finally the shorturl.php file has some helper functions for shortening FlatPress IDs. The helper functions were taken from this StackOverflow question.
Lets look at the configuration template first. The plugin needs to know the short URL domain in order to generate short links, so that has to be configurable. There is a very easy to use API for adding plugin options so I used that, but it requires a template to generate the config screen.
This creates a screen that look like this (assuming you have the same theme as me):
Next, the plugin.shorturl.php file adds a filter called short_post_link which eventually calls the psh_shorturl function to generate the short URL. The rest of the code here sets up the admin configuration screen and handles saving of the plugin options.
The redirect.php file uses the short ID passed in as a URL parameter to get back the original FlatPress ID, it then looks up the post in the FlatPress database and gets it's permalink. Then the browser is redirected to this long URL (permalink).
...and finally the shorturl.php script has some helper functions that converts the base-10 ID to a base-62 ID. To make it a little interesting, I shuffled the base-62 character locations from their standard arrangement.
Now the plugin is ready it's time to configure it and add some lines to the .htaccess file. Make sure that you enable the plugin first, after that is done its configuration option will appear. Enter the domain name to use for the short URLs and click Save.
The configuration screen will display the exact lines that you'll need to add to the .htaccess file, the format for these is as below where SHORT_DOMAIN is your short domain name and the LONG_DOMAIN is the blog actual domain name.
To generate a short URL, add this to the FlatPress theme template wherever you want the short URL to appear (within the entry block):
One other thing I did was tweak the Pretty URLs plugin to the the FlatPress Blog URL instead of the base URL, without this change redirection will not work properly. An alternative solution to that is to have this plugin keep track of the long URL. The change is in the get_url() function, instead of setting $baseurl to BLOG_BASEURL, I set it to $fp_config['general']['www'].
Now you're done.