Advertisement:

Author Topic: URL Shortner  (Read 1974 times)

mikestar

  • Guest
URL Shortner
« on: February 12, 2013, 11:42:40 pm »
Hey everyone,

Is anyone working on a URL shortner plugin for OSclass?

This would be a really great addition to the platform, so when a user copies the URL is automatically shortens. (not sure if this is even possible)

Regards
Mike

Khalid

  • Newbie
  • *
  • Posts: 45
  • WEBNET® Comunicaciones
Re: URL Shortner
« Reply #1 on: February 13, 2013, 12:41:58 am »
Hello, actually I'm using a simple function that uses tinyurl.com API to make short urls, I've been using it for quite a while now and not only for osclass but on many of my websites that support sharing posts systems.

To implement it, just open your functions.php on your theme's main folder and paste the following code:

Code: [Select]
/*-----------------------------------------------------------------------------------*/
/* URL Shortener
/*-----------------------------------------------------------------------------------*/

    function wnc_shorturl($url)  { 
        $ch = curl_init(); 
        $timeout = 5; 
        curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url); 
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
        $data = curl_exec($ch); 
        curl_close($ch); 
        return $data; 
    }

To use this function on an item just write the following to display the shortened url for it:

Code: [Select]
<?php echo wnc_shorturl(osc_item_url()); ?>
Aditionally you can display it into a disabled text field (just like YouTube) and make selectable using JavaScript if you want, here's the code:

Code: [Select]
<input id="txtfld" class="itemlinkspace" type="text" readonly="readonly" value="<?php echo wnc_shorturl(osc_item_url()); ?>" onClick="SelectAll('txtfld');" />
The JavaScript is as follows (paste it just after the code above):

Code: [Select]
<script type="text/javascript">
    function SelectAll(txtfld)
    {
       document.getElementById(txtfld).focus();
       document.getElementById(txtfld).select();
    }
</script>

Hope that helps.




mikestar

  • Guest
Re: URL Shortner
« Reply #2 on: February 13, 2013, 03:25:58 pm »
Hey Khalid,

Nice one.

I will give it a go and let you know the results.

Thanks

Khalid

  • Newbie
  • *
  • Posts: 45
  • WEBNET® Comunicaciones
Re: URL Shortner
« Reply #3 on: February 16, 2013, 11:21:53 pm »
Hey Khalid,

Nice one.

I will give it a go and let you know the results.

Thanks

Anytime mike, and yes please, let me know the result.

Good luck!