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>

Posted by in API, PHP, WordPress on April 19, 2008
  • Pingback: PHP Weekly Reader - April 20th 2008 : phpaddiction

  • http://www.cagintranet.com/ Chris

    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/

  • http://beyondrandom.com BeyondRandom

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

  • http://www.PuppyWebsite.com Tom (Puppy Website)

    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..

  • http://www.aquariumfish.me mrgtb

    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.

  • http://www.raaj.com.np www.Raaj.com.np

    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

  • http://www.mrgtb.com mrgtb

    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.

  • http://kovshenin.com kovshenin

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

  • http://www.velvetblues.com Shirley

    Great code snippet. :-)
    Thanks for sharing.

  • http://www.websitedevelopmentology.com Website Development

    Very interesting! I never knew you could so easily integrate that into a WP blog, I will surely put this onto my blog.

  • Joana

    this page gave me lots of idea, i’m just a begginer but i feel like i’ve learned a lot with this site.. thanks! i will surely recommend this to my friends..

  • http://www.jjtcomputing.co.uk Jonathan Ellse

    Great tutorial, but it is difficult to know where you instruct one to put the code blocks (eg header.php, single.php, index.php). Where do you put the 2nd and 3rd code blocks?

    Thanks,

    Jonathan

  • http://www.jjtcomputing.co.uk Jonathan Ellse

    Also, I get an error in line 12 of the first block when I put it in a file surrounded by <?php ?> brackets. ‘Unexpected character inline 12 -”\” …’ How can I sort this?

    Thanks, Jonathan

  • http://www.webmastersgallery.com/ Web Development Blog

    This is Very nice blog-in and also very needful for me…..

  • http://www.redips.net/ Darko Bunic | www.redips.net

    Very useful and I hope your function is free to use :) Just to mention that WordPress site displays curly ‘smart quotes’. Smart quotes are nice, but not suitable for program code, because copy&paste will not just work. I had to manually fix quotes …

    Anyway, thanks for nice tip.

  • http://www.crearedesign.co.uk Robert K

    Damn those smart quotes! Many a day have I had to find-replace those…Nice little Digg function by the way! Custom built is always better than tinkering with someone elses code :)

  • http://mapofcrisis.com//story.php?title=mr--capone-e-lyrics-ringtones-music-videos-clothing-biography-mp3-downloads-dvds-albums-discography Nestor

    How long did it take you to write this blog.

  • http://www.4heroes.co.uk Robin Johnson

    Great post.

    You say it could easily be moddified for other use than WordPress? I do use wordpress but also have many other websites in which this would come in handy.

    How easy would it be to work into a Java based Ruby on Rails website?

    ————————————————–

    http://www.4heroes.co.uk
    Website Design that gives 30% to Charity!

  • http://www.cammozaik.org cam mozaik

    Thanks a lot! I am just learning Information.
    Php and this was very easy to follow and helped a lot.
    You really took time to explain every little bit.
    Thanks again..

  • http://www.registrycleaner.at/ Registry Cleaner Reviews

    very well

    information you write it very clean. I'm very lucky to get this information from you.

  • WordPress Shopping Cart

    Nice, having a counter is very fun to add to your site.

  • http://webmama.co.uk/ Ecommerce Software

    Wow it is amazing.I think I should try it into my
    <a href="http://www.webmama.co.uk/seo_directorysubmission.html">Ecommerce Website</a> that it can get a lot of visitors..
    Thanks for this blog.I'll share it with my facebook friends.

  • http://theweb-coder.com Masud Ibn Afjal

    Excellent work ..Thanks a lot

  • Anonymous

    just test something

  • http://www.im3.co.uk/ SEO Sheffield

    How do I create a static wordpress page that I can edit in dreamweaver?

  • Bahlawi89

    Thanks Man for your great job.

    http://www.tec-world.info 

    I will paraphrasing and post in my website and backlink to you 

  • http://www.cyberdesignz.com/ Web Page Design Company

    wow! i really need it and finally got it after long time.
    Thanks for sharing