tags:

views:

33

answers:

2

I was wondering if it was possible to add a api to wordpress to make all the links in a post a shortened url (in this case for linkbee - http://linkbee.com/api%5Fdoc.php) if so how would i do this?

A: 

Yeah, you can do that with WordPress. I made a similar (very simple) plug-in that lets me do backtick style editing that later gets highlighted as CODE tags, before StackOverflow.com was around too! You can find my code in a Bazaar repository at http://santiance.com/bzr/wp-backticks but here's the source minus the meta information:

add_filter('the_content', 'backticks_filter_post', 5);

function backticks_filter_post($post) {
    $post = preg_replace('/[“”]/i', '"', $post);
    return preg_replace('/`([^`]+)`/i', '<code>$1</code>', $post);
}

Here you just have to modify my backticks_filter (probably should rename it too) function to translate your URLs into tiny URLs. For performance you can store this in a cache the first time or when the post is submitted in WordPress as a custom field.

Kristopher Ives
im sorry but i have no clue how to use this, im do only front dev. Can you elaborate a little more please?? it would really help
delboud
A: 

You can use the PHPShortener library to create a plugin that filters the_content filter in WordPress:

class Foo_UrlShortener_Plugin {
    const SERVICE = 'is.gd';
    private $_shortener;

    public function __construct() {
        $this->_shortener = new PHPShortener();
        add_filter('the_content', array($this, 'filterContent')); 
        add_filter('the_content_rss', array($this, 'filterContent')); 
    }

    public function filterContent($content) {
        return preg_replace_callback('#(http://\S+)#', array($this, 'handlePregCallback'), $content);
    }

    public function handlePregCallback($matches) {
        return $this->_shortener->encode($matches[1], self::SERVICE);
    }
}

Basically this searches for anything that starts with http://, captures it, then replaces it with the shortened version.

Chancey Mathews