How to use the bit.ly URL shortener in leenk.me

I just submitted version 1.2.11 to the WordPress repository, so you should see this update coming soon. With this update is the ability to customize your URL shortener. If the URL shortener you use has a basic REST API then it should work.

For now, leenk.me uses a custom WordPress action hook to allow you to have complete freedom of what URL shortener you use. The most common request I get is for bit.ly, so here are the instruction for switching to the bit.ly URL shortener.

First, update to leenk.me 1.2.11!

Second, add this code near the top of your theme’s function.php file (after the <?php line):

function custom_leenkme_url_shortener( $api_url, $url ) {

    $bitly_api ="http://api.bitly.com/v3/shorten";

    $bitly_args = array(
                            'login' => 'ENTER_YOUR_BITLY_USERNAME',
                            'apiKey' => 'ENTER_YOUR_BITLY_API_KEY',
                            'longURL' => $url,
                            'format' => 'txt',
                            'domain' => 'bit.ly' // either bit.ly or j.mp
                        );

    return $bitly_api . '?' .  http_build_query( $bitly_args );

}
add_filter( 'leenkme_url_shortener', 'custom_leenkme_url_shortener', 10, 2 );

Third, make sure you replace “ENTER_YOUR_BITLY_USERNAME” with your bit.ly username and “ENTER_YOUR_BITLY_API_KEY” with your bit.ly API key.

This should do it, your URLs should be automatically shortened with your bit.ly account.

If you want to use another URL shortener, contact me and I’ll try to get you some code to use instead of the bit.ly example above.

This entry was posted in Custom URL Shorteners, News, plugin, Twitter and tagged , , . Bookmark the permalink.


15 Responses to How to use the bit.ly URL shortener in leenk.me

  1. Claude Charpentier says:

    Thanks a lot for this hook !

    zat

  2. Adrian Arias says:

    I must be doing something wrong…. I opened my funtions.php file at top and I see this <?php do I put the code here? and also when i put my user name, do I leave the parenthesis (') the 'username'. or with out it.

  3. Lew Ayotte says:

    Hi Adrian,

    Yeah, right below the first <?php line should be fine. You leave the single quote marks around the username and password.

    Let me know if you need further assistance.

    Lew

    • Adrian Arias says:

      Thanks Lew, but also I need the bit.ly link to be sent to my facebook. Right now is only sending it to twitter, how do I do that?

      • Adrian Arias says:

        OH! I also try using %URL% so it send the short link to facebook but it didn’t work.

        • Lew Ayotte says:

          Hi Adrian,

          It doesn’t do this by default, actually the next major release is suppose to do this automatically and the option to do bit.ly/su.pr/etc. will be built into the User Interface.

          So, if you want to do this, you’ll have to update the actual plugin code. Update to the latest version then edit facebook.php. Go to line 648 and change this line:

          $url = get_permalink( $post->ID );

          to this:

          $url = leenkme_get_shortened_url( get_permalink( $post->ID ), $post->ID );

          That should do the trick. If you update the plugin anytime after you edit that code, you’ll need to do it again… until the next major release is released. The version that will include this is leenk.me 2.0. Let me know if you have any questions.

          Lew

          • Adrian Arias says:

            Lew is not working, I changed line 648 but when i go to facebook to see the post still shows the long url. here u can see my fanpage –> facebook.com/tr3flow

  4. Adrian Arias says:

    Lew is not working, I changed line 648 but when i go to facebook to see the post still shows the long url. here u can see my fanpage –> facebook.com/tr3flow

    • Lew Ayotte says:

      Hi Adrian,

      It’s working fine for me. Can you email me your functions.php file? and your facebook.php file?

      Lew

      • Adrian Arias says:

        Ok Lew i send it thru your contact form

      • Adrian Arias says:

        I put the file you sent me but now wen click publish Im getting this error –>
        Fatal error: Call to undefined function leenkme_get_shortened_url() in /var/www/vhosts/lacoquillita.com/httpdocs/v3/wp-content/plugins/leenkme/facebook.php on line 649

  5. VBK says:

    How about using a own Link shortner based on yourls?

    • Lew Ayotte says:

      Hey VBK, In the latest version of the leenk.me plugin, all of this is built into the GUI. It even has YOURLS support built in. But if you have on that isn’t built into the plugin, you can use this same functionality above (with some minor modifications) to add your own custom link shortener.