19
Apr

You will notice in the lower right corner of this entry that there is a reference to the number of Diggs for this post. If you click the counter you are taken to Digg. Digg does offer a few easily integrable scripts at http://digg.com/tools/integrate but does not document how to create a custom counter. To accomplish this you need to use the Digg API which is very easy to work with. There is no registration and your access key is simply the source domain. This example is customized for WordPress but can easily be modified to work in any PHP based project.

Here is the PHP function to grab the number of Diggs from the Digg API.

function getDiggs($url) {
$sturl = 'http://services.digg.com/stories?link='.$url.'&appkey=
http%3A%2F%2Fwww.yourdomain.com&count=1';
$ch = curl_init($sturl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,"www.yourdomain.com");
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$res = curl_exec($ch);
$resinfo = curl_getinfo($ch);
curl_close($ch);
if($resinfo['http_code'] === 200) {
preg_match(’/diggs\=”([0-9]+)”/si’, $res, $r);
$diggs = number_format(($r[1]) ? str_replace(’,', ”, $r[1]) : ‘0′);
if($diggs == 1) {
$response = $diggs .’ Digg’;
} else {
$response = $diggs .’ Diggs’;
}
} else {
$response = ‘Error’;
}
echo $response;
}

If you plan on displaying your Diggs on multiple pages create a folder in your theme directory and include the file with the following…

<?php include(TEMPLATEPATH .'/folder/script.php'); ?>

Setup the variables to build the Digg link and the URL to pass to the PHP function. This will use the current post URL in a loop or on a standalone page so there is nothing to hardcode.

$diggurl = urlencode(get_permalink($post->ID));
$diggtitle = urlencode(get_the_title($post->post_parent));
$digglink = 'http://digg.com/submit?url='.$diggurl.'&title='.$diggtitle;

Finally add the link, in my case I have positioned the reference in the bottom right of the post and added a bubble comment background to the link with CSS. In the href I am passing the encoded link and the encoded title to Digg. You can also pass through the media type, description, and topic if you want. Click here for details.

<a href="<?php echo $digglink ?>" class="diggticker"><?php getDiggs($diggurl); ?></a>



74 Diggs Spread This

17 Responses


Thank you very much for the Tutorial.

Franka on 20 Apr 2008 at 8:05 am

[...] Custom Digg Counter in Wordpress with PHP — Digg just gets on my nerves but that doesn’t mean i won’t need this someday for somebody. [...]

PHP Weekly Reader - April 20th 2008 : phpaddiction on 24 Apr 2008 at 12:22 am

Very nice… I could have used this a while back, but instead i created a WP plugin that does this for you as well…. take a look:

http://www.cagintranet.com/archive/wp-socialcount-plugin/

Chris on 29 Apr 2008 at 5:48 am

Great Tutorial! Gonna have to get started on mine now!!

BeyondRandom on 29 Apr 2008 at 6:45 am

cool, Ill try on my site now. Thanks :)

fedmich on 04 Jun 2008 at 6:36 pm

I just wanted to say thank you very much for this great tutorial..I found it to very informational, it will certainly provide help in programming.
I’ve been searching for a way to do this weeks ago..

Tom (Puppy Website) on 30 Jun 2008 at 1:26 am

thanks alot bro for such a great post.

moin on 13 Jul 2008 at 2:25 pm

Do you have a working version of this, that can be used in HTML pages instead of PHP pages, that will grab the page “URL and Title” automaticly like this script does for php pages.

mrgtb on 06 Sep 2008 at 8:17 pm

For “mrgtb on 06 Sep 2008 at 8:17 pm ”
————————————————
Before asking working version of this script in HTML instead of PHP you first need to know what is the difference between HTML and PHP.

Please try to know this stuff through
http://www.google.com/search?hl=en&q=difference+between+html+php

Good Post Michael

www.Raaj.com.np on 13 Sep 2008 at 6:50 pm

OK, I was aking because I have been seaching google for ages a few times to try and get a custom digg counter like this to use with HTML pages. But no luck at all.

In fact this is the only custom Digg counter I have found.

mrgtb on 07 Oct 2008 at 6:12 am

Не пойму в чём дело, но у меня тока 2 картинки загрузилось. ((( А ваще понравились! :)

gordav on 06 Nov 2008 at 11:48 pm

да,но это еще и не все…

DpyuD on 07 Nov 2008 at 2:48 pm

“Очень хороший”

black on 08 Nov 2008 at 1:52 am

Very helpful. Thank you ;) I’ll make sure to use that on my blog =)

kovshenin on 10 Nov 2008 at 7:23 am

“Полезного много”

bsi on 18 Nov 2008 at 5:44 pm

“шикарно”

Bus on 18 Nov 2008 at 9:21 pm

“Всегда приятно читать умных людей”

andry1 on 19 Nov 2008 at 6:29 am
Trackback URI | Comments RSS

Leave a Reply

Subscribe to RSS Feed
Powered by FeedBurner
Sponsors
Recent Links