getButterfly Logo getButterfly

I recently needed to extract the Klout score for a WordPress plugin, and I coded a quick shortcode. I can already think of several improvements, such as transient (or fragment) caching, or showing changes (increase or decrease) from yesterday, last week and last month (because the returned data includes all these), but for now, I only need the basic functionality.

Here’s the code, if you need it, and here’s the shortcode, packaged as a WordPress plugin:

function k2api_klout($atts, $content = null) {
        'prepend' => '',
        'type' => 'score',
        'key' => 'kqu4t7p74bxq7skynmn7zdwu',
        'user' => 'getbutterfly',
    ), $atts));

    $url_user_id = '' . $user . '&key=' . $key . '';
    $data_user_id = json_decode((file_get_contents($url_user_id)));
    $klout_user_id = $data_user_id->id;

    $url_kscore = '' . $klout_user_id . '/score?key=' . $key . '';
    $data = json_decode((file_get_contents($url_kscore)));

    if ($data) {
        $kscore = $data->score;
    } else {
        $kscore = "?";

    $out = '';

    $out .= '<div class="klout-score">
        <p>' . $prepend . '<a href="' . $user . '">' . round($kscore) . '</a></p>

    return $out;

add_shortcode('klout', 'k2api_klout');

Note that you will need a Klout developer account.

This is how you use it in a post, page or widget:

[klout type="score" key="kqu4t7p74bxq7skynmn7zdwu" user="getbutterfly" prepend="Klout Score: "]

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

Leave a Reply

Your email address will not be published. Required fields are marked *