Quantcast
Channel: TaimoorSultan.com » Tutorials
Viewing all articles
Browse latest Browse all 18

Bit.ly as MyBB Url Shortener

$
0
0

MyBB is an open source forum software and is very popular. Today I’m sharing a way to use Bit.ly as a URL shortener for MyBB. Bit.ly is a website providing services for shortening Urls on site and through Api. As we know normal Urls could be so long that they cannot be shared on twitter or chats etc. So, we need to use a Url shortener. Today I’m writing about a way to use Bit.ly and place a button on your MyBB forum header menu. Every time a user clicks that button, a popup will appear containing the shortern Url for that page. This url can be then shared with ease. A big advantage of using bit.ly is that they provide easy stats for your shorten links. So, let me teach you how to use Bit.ly as MyBB Url Shortener.

Demo of Bit.ly as Mybb Url Shortener – Live Demo

bit.ly as MyBB Url Shortener

bit.ly as MyBB Url Shortener Popup

Bit.ly as MyBB Url Shortener Setup

1. Go to Bit.ly and register for an account.

2. After registration, go to this page and get your Api key.

3. Edit the file “Global.php” using Dreamweaver or Notepad. And Paste this code right after the first <?php

//Shorten URL Code Starts
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
function make_bitly_url($url,$format = 'xml',$version = '2.0.1')
{
//Set up account info
$bitly_login = 'YOUR_USER_NAME_HERE';
$bitly_api = 'YOUR_API_KEY_HERE';
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$bitly_login.'&apiKey='.$bitly_api.'&format='.$format;
//get the url
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //For XML
{
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
$full_url = curPageURL();
$short_url = make_bitly_url($full_url);
//Shorten URL Code Ends

4. After pasting above code right after the first <?php tag, now edit the “Setup the account info” portion of above code and enter your Username and Api key, which you got from Bit.ly website, in Step 2. After completion, save “Global.php”.

5. Now, in Admin CP of MyBB, go to Templates & Styles –> Templates –> Default Template –>Header Templates –> Header.

6. With in the <ul> tag of <div class=”menu”>, we need to add another <li> which will contain code for the shortening current page url. Now, Paste this code before all <li> tags with <ul> tag of <div class=”menu”>.

<li><a href="#" onclick="prompt('Shorten URL', '<?php echo $short_url; ?>');">Shorten URL</a></li>

7. Now save the header and preview your Forum, you will see another link name “Shortern Url” in the header menu. Click that and a popup will appear containing shorten url of that current page and this was what we exactly want.

8. The actual piece of code which displays the current page Url is below, and you can paste it any where in the template to display current page Url.

<?php echo $short_url; ?>

So, guys this was all about using Bit.ly as Url Shortener for Mybb, you may use this method with other platform as WordPress but you will need little modifications, I will surely write about that too. Bye!

The post Bit.ly as MyBB Url Shortener appeared first on TaimoorSultan.com.


Viewing all articles
Browse latest Browse all 18

Latest Images

Trending Articles



Latest Images