I recently purchased the somad.es domain for use with my own URL shortener. (Vanity or owning my own URLs: you decide.) I decided to use Alan Hogan’s Lessn More, an improved fork of Shaun Inman’s Lessn. I then decided I wanted to integrate the shortlink into my WordPress install, but the existing WP Lessn plugin isn’t very good. Its admin page doesn’t adhere to WP security best practices, it doesn’t use WP’s HTTP API, and it doesn’t properly integrate with WP 3.0’s shortlink API. In short, it doesn’t do things the WP way.
So, I made my own little plugin. I didn’t want another plugin settings page, so you have to hardcode your Lessn URL and API key. It integrates with the “Get Shortlink” button on your write/edit post screen, and the relevant shortlink HTTP header and meta elements are auto-inserted. Only the “post” post_type will get Lessn’d by default, but see the bottom of the plugin for how to add additional post_types.
<?php/* Plugin Name: Lessn Shortlinks Description: Integrates WP's shortlink functionality with the <a href="http://lessnmore.net/">Lessn More</a> URL shortener. <em>Hasn't been tested with vanilla Lessn, but the API should be compatible.</em> (Edit the file manually to configure) Version: 1.0 Author: Matt Wiebe Author URI: http://somadesign.ca/*/
/** * SD_Lessn class: integrates WP's shortlink functionality with the Lessn More * URL shortener. * * @uses Lessn More @link http://lessnmore.net/ * @author Matt Wiebe * @license GPL 2 * **/
class SD_Lessn {
const API_KEY = 'apikey'; // your Lessn API key const LESSN_API_BASE = 'http://shorturl.com/-/'; // the API base for your Lessn install const META_KEY = '_lessnd_url';
private $allowed_types = array('post');
function __construct() { add_action('publish_post', array($this, 'get_lessnd_url'), 10, 1 ); add_filter('get_shortlink', array($this, 'get_shortlink'), 10, 3 ); }
public function add_post_type( $post_type = '' ) { if ( $post_type ) $this->allowed_types = array_merge( $this->allowed_types, (array) $post_type ); }
public function get_lessnd_url( $post_id ) { if ( 'publish' !== get_post_status($post_id) ) return false;
if ( ! in_array( get_post_type($post_id), $this->allowed_types ) ) return false;
if ( $url = get_post_meta( $post_id, self::META_KEY, true) ) return $url;
$params = array( 'api' => self::API_KEY, 'url' => get_permalink($post_id) ); $api_url = add_query_arg($params, self::LESSN_API_BASE);
$get = wp_remote_get($api_url);
if ( is_wp_error($get) ) return false;
$lessnd_url = wp_remote_retrieve_body($get);
if ( $lessnd_url ) { update_post_meta( $post_id, self::META_KEY, $lessnd_url ); return $lessnd_url; } return false; }
/** * @param int $id A post or blog id. Default is 0, which means the current post or blog. * @param string $contex Whether the id is a 'blog' id, 'post' id, or 'media' id. If 'post', the post_type of the post is consulted. If 'query', the current query is consulted to determine the id and context. Default is 'post'. */
public function get_shortlink( $shortlink, $id, $context ) {
if ( 'query' == $context ) { global $wp_query; if ( is_singular() ) { $context = 'post'; $id = $wp_query->get_queried_object_id(); } }
if ( 'post' !== $context ) return $shortlink;
// Try Lessn'd url. if ( $try_lessn = $this->get_lessnd_url($id) ) return $try_lessn;// do_action('elog', $shortlink); return $shortlink; }}
$sd_lessn = new SD_Lessn;
// uncomment line below to add support for the "page" post_type// $sd_lessn->add_post_type('page');Now, I just need to figure out how to integrate shortlinks for humans rather than just machines.
2 Comments
Hi Matt,
I’m trying to get your plugin to work with my site, and whenever I click the Get Shortlink button, the content of the shortlink is my 404 page.
I added the API key and the API base.
My lessn is installed at http://example.com/u/ and the backend of it is at http://example.com/u/-/ … so my API base is:
const LESSN_API_BASE = ‘http://example.com/u/-/’; // the API base for your Lessn install
Am I missing something?
Thanks so much!!
That seems like it should work. Have you gotten a link to shorten properly via the API call otherwise?
One Trackback