Create Your Own Twitter Share Button

As you can see on WordPress.org plugin page, there is a lot of Twitter share button which you can use, but all of them already valid, you cannot change their styling easily.

So, right now i will tell you how to create it with your own hands, sure you can do a styling on it.


<a href="http://twitter.com/share?text=<?php the_title(); ?>&url=<?php the_permalink();?>" target="_blank"><span>Twitter</span><i></i></a>

At that code, i using several CSS layers to make it more beautiful.

Then, to displaying the counter of how many url shared, you can open your functions.php or theme-functions.php and put this code:


function get_tweets($url) {
 $json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
 $json = json_decode($json_string, true);
 return intval( $json['count'] );
}

Then, to call the counter, you can simply put this code at your page.php or single.php:


<?php echo get_tweets(get_permalink($post->ID)); ?>

Done! You already create your own Twitter button

 

Advertisements

5 thoughts on “Create Your Own Twitter Share Button

  1. This can be actually serious, You’re an incredibly efficient writer. I’ve joined with your feed and also will enjoy finding your outstanding write-ups. In addition, I’ve shared your blog post with our social networking sites.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s