Not signed in ( Sign In)

Categories

Welcome, Guest

Want to take part in these discussions? Sign in if you have an account, or apply for one below

Vanilla 1.1.10 is a product of Lussumo. More Information: Documentation, Community Support.

    •  
      CommentAuthorpaul
    • CommentTimeAug 25th 2009 edited
     permalink
    I created a little plugin (workaround) to enable short links on a flatpress blog to use for example on Twitter. Not sure if anyone is interested, but I announce it here anyway.

    The design is not really nice as this plugin builds the URLs to redirect for itself instead of calling a core function of flatpress doing this (is there a function like this?). But it works well, does not matter if you use pretty URLs or not. The short link will redirect you per header redirect to the right URL.

    To make it work the .htaccess which is used by the pretty URLs plugin, has to be active, of course.

    <?php
    /*
    Plugin Name: shorturls
    Version: 0.1
    Plugin URI: http://blog.wbou.de
    Author: Paul Wellner Bou
    Author URI: http://blog.wbou.de
    */

    define('PAULWB_PLUGIN_SHORTURL', true);

    add_action('init', 'plugin_shorturls_head', 0);
    add_action('entry_block', 'plugin_shorturls_calc');

    function plugin_shorturls_calc() {
    global $fpdb, $smarty;

    $q = $fpdb->getQuery();
    $id = $q->currentid;
    $htmlLinkStr = "<div class=\"shortlink\">[<a href=\"/".getShortId($id)."\">Short Link</a>]</div>";
    $smarty->assign('shortlink', $htmlLinkStr);
    }

    function plugin_shorturls_head() {

    $url = $_SERVER['REQUEST_URI'];
    if(!preg_match("/^\/[a-z0-9]{1,5}$/", $url))
    return;

    $shortUrlStr = str_replace("/", "", $url);
    $o = new FPDB_Query(array('start'=>0,'count'=>-1,'fullparse'=>false), null);

    while ($o->hasMore()) {
    list($id, $contents) = $o->getEntry();
    if($shortUrlStr == getShortId($id)){
    // use pretty urls, if plugin available
    if(defined('PRETTYURLS_TITLES')){
    $url = _permalink($id, $contents['subject']);
    }else{
    $url = BLOG_BASEURL . "?x=entry:$id";
    }
    header("Location: $url");
    die();
    }
    }
    }

    # copied and adjusted from plugin.prettyurls.php
    function _permalink($id, $title) {
    if (PRETTYURLS_TITLES)
    $title = sanitize_title($title);
    else
    $title = $id;
    $date = date_from_id($id);
    return BLOG_BASEURL . "20{$date['y']}/{$date['m']}/{$date['d']}/$title/";
    }

    function getShortId($fullid) {
    return base_convert(getNumericId($fullid), 10, 36);
    }

    function getNumericId($fullid) {
    $numid = substr($fullid, strpos($fullid, "-")+1);
    $numid = intval($numid);
    return $numid;
    }

    ?>


    You can test it here: http://blog.wbou.de

    Of course, if you want to output the hyperlink within the blog entry, the template has to be modified, putting

    ...
    {$shortlink}
    ...


    Where the <a href...> should appear.