Skip to content

Instantly share code, notes, and snippets.

@monecchi
Forked from theukedge/gist:6746856
Last active May 6, 2016 21:34
Show Gist options
  • Save monecchi/f5f39ebeb376511bc1f5b28d73b7b725 to your computer and use it in GitHub Desktop.
Save monecchi/f5f39ebeb376511bc1f5b28d73b7b725 to your computer and use it in GitHub Desktop.
/*
* TWITTER SHARE BUTTON WITH Bit.ly SHORT LINK SUPPORT
* First, you need to create a function to tell WordPress to get a short URL for your post.
* To do this, you will need a bit.ly account. Once you’ve created an account, go to your Account Settings
* and take note of your API key and your username.
* Now open your functions.php file, scroll all the way to the end and paste in the following code,
* substituting in your username and API key for yourbitlyusername and yourbitlyAPIkey:
*/
<?php
function getBitly($url) {
$bitly = file_get_contents("http://api.bit.ly/v3/shorten?login=yourbitlyusername&apiKey=yourbitlyAPIkey&longUrl=$url%2F&format=txt");
return $bitly;
}
?>
<?php
function twitter_share_btn($url) {
//$twitter_user = get_option('twitter_user', '1'); // get username from the theme's options
$twitter_user = '@dico_monecchi'; // manually set the username
$bitly = getBitly(get_permalink($post->ID));
echo '<a class="" href="http://twitter.com/home/?status='. the_title() .', '.$bitly.' via '.$twitter_user.'" title="Tweet this!"></a>';
}
?>
<?php echo twitter_share_btn(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment