WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Sociable] Adding Bit.ly URL Shortener (1 post)

  1. reeley
    Member
    Posted 4 years ago #

    I thought it would be nice to be able to shorten my wordpress url's with bit.ly. I am already a bit.ly user and haven't used awe.sm. Here is how I implemented it (this is a diff of the sociable.php [the one with the new features] and sociable.php.bak [the original]). This also lets enable this feature, add your API key and Username in the sociable settings page.

    diff -b sociable.php.bak sociable.php
    743a744,769
    >
    >         // Bit.ly the Permalink if bit.ly is enabled
    >         if (get_option('sociable_bitly') == true) {
    >                 $pageaddress = urlencode(get_permalink($post->ID));;
    >                 $bitlyusername = get_option('sociable_bitlyusername');
    >                 $bitlyapikey = get_option('sociable_bitlyapikey');
    > 		$bitlyurl = "http://api.bit.ly/shorten?version=2.0.1&longUrl=$pageaddress&login=$bitlyusername&apiKey=$bitlyapikey";
    >         	$curl_handle = curl_init();
    >         	curl_setopt($curl_handle, CURLOPT_URL, "$bitlyurl");
    >         	curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    >         	curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    >         	$response = curl_exec($curl_handle);
    >         	curl_close($curl_handle);
    >         	// parse the response
    >         	$bitly = json_decode($response, true);
    >         	// if there are no errors set the permalink to the shortUrl
    > 		if ($bitly["errorCode"] == 0) {
    >                   // everything went fine set the permalink to the bit.ly address
    >                   $permalink = urldecode($pageaddress);
    >                   $permalink = $bitly["results"]["$permalink"]["shortUrl"];
    > 		} else {
    >                   // if there is an error then fallback to the long permalink
    >                   $permalink 	= urlencode(get_permalink($post->ID));
    >                 }
    > 	} else {
    >                 // if bit.ly isn't enabled then get the long permalink
    744a771,772
    > 	}
    >
    1153c1181
    < 		foreach ( array('usetargetblank', 'useiframe', 'disablealpha', 'disablesprite', 'awesmenable', 'usecss', 'usetextlinks', 'disablewidget') as $val ) {
    ---
    > 		foreach ( array('usetargetblank', 'useiframe', 'disablealpha', 'disablesprite', 'awesmenable', 'bitly',  'usecss', 'usetextlinks', 'disablewidget') as $val ) {
    1171c1199
    < 		foreach ( array('awesmapikey', 'tagline', 'imagedir') as $val ) {
    ---
    > 		foreach ( array('awesmapikey', 'tagline', 'imagedir', 'bitlyusername', 'bitlyapikey') as $val ) {
    1198c1226,1244
    < 		sociable_message(__("Saved changes.", 'sociable'));
    ---
    >                // Checking to see if  there are any bit.ly errors to display when settings are saved.
    >                // Add an authentication check here too...
    >                if (get_option('sociable_bitly') == true) {   // is bitly enabled?
    >                   if (get_option('sociable_bitlyusername') == "") {  // is the username field empty?
    >                     $bitlyerror = "  Bit.ly will not work correctly.<br />Please check your Bit.ly username.";
    >                   }
    >                   if (get_option('sociable_bitlyapikey') == "") {  // is the API key field empty?
    >                     if ($bitlyerror == "") {  // is an error already saved ?
    >                       $bitlyerror = "  Bit.ly will not work correctly.<br />Please check your Bit.ly API Key.";
    >                     } else { // this is our second error.
    >                       $bitlyerror = "  Bit.ly will not work correctly.<br />Please check your Bit.ly Username and API Key.";
    >                     }
    >                   }
    >                 } else {
    >                   $bitlyerror = "";
    >                 }
    >
    >
    > 		sociable_message(__("Saved changes. $bitlyerror", 'sociable'));
    1369a1416,1426
    > 			<?php _e("bit.ly:", "sociable"); ?>
    > 		</th>
    > 		<td>
    > 			<?php _e("You can have your links automatically shortened by bit.ly if you provide your username and API key.  You should only use bit.ly if you are not using awe.sm.", 'sociable'); ?><br />
    > 			<input type="checkbox" name="bitly" <?php checked( get_option('sociable_bitly'), true); ?> /><?php _e("Enable bit.ly?", "sociable"); ?><br />
    > 			<?php _e("bit.ly username:", 'sociable'); ?><input size="10" type="text" name="bitlyusername" value="<?php echo get_option('sociable_bitlyusername'); ?>" /><br />
    > 			<?php _e("bit.ly API Key:", 'sociable'); ?><input size="60" type="text" name="bitlyapikey" value="<?php echo get_option('sociable_bitlyapikey'); ?>" />
    > 		</td>
    > 	</tr>
    > 	<tr>
    > 		<th scope="row" valign="top">
    1373c1430
    < 			<?php _e("You can choose to automatically have the links posted to certain sites shortened via awe.sm and encoded with the channel info and your API Key.", 'sociable'); ?><br />
    ---
    > 			<?php _e("You can choose to automatically have the links posted to certain sites shortened via awe.sm and encoded with the channel info and your API Key.  You should only use awe.sm if you are not using bit.ly.", 'sociable'); ?><br />

    I would have liked to have changed this to include a radio button choice between url shortening disabled, bit.ly, and awe.sm but I just stopped working on it. I hope this is something that blogplay can implement.

    Thanks,
    ~Josh

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 4 years ago by reeley
  • This topic is not a support question
  • WordPress version: 2.9.2