Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Embed Stackexchange Account Metrics into Wordpress?

How can I embed Stackexchange reputation scores and badge counts into my Wordpress blog? I want to show on my blog a small table with accounts as rows and columns consisting of rep scores and badge counts. Any ideas on how to do this?

like image 344
Chernoff Avatar asked Dec 16 '22 07:12

Chernoff


2 Answers

You could you use User Flair for this:

https://stackoverflow.com/users/flair

like image 57
Brian Avatar answered Mar 12 '23 08:03

Brian


Here's a testing code I already had for consuming Stack Exchange API:

<?php
/**
 * Plugin Name: Print SE-API Results as Admin Notice
 */

add_action( 'admin_notices', 'b5f_consume_se_api' );

function b5f_consume_se_api() 
{
    $user = '1417894';
    $page_size = '&pagesize=3';
    $order = '&order=desc';
    $sort = '&sort=votes';

    $so = wp_remote_get( 
        'http://api.stackexchange.com/2.1/users/'
        . $user
        . '/answers?site=stackexchange'
        . $page_size . $order . $sort ,     
        array(
            'timeout'     => 120, 
            'httpversion' => '1.1' 
        ) 
    );

    if ( $so['response']['code'] == '200' )
    {
        $so_array = json_decode( $so['body'], true );
        var_dump( $so_array['items'] );
    }
}

This is the URL being consulted and its JSON result. It returns the last 3 answers from the OP, sorted by votes (descending).

Check the docs and adapt everything to suit your needs.

like image 38
brasofilo Avatar answered Mar 12 '23 08:03

brasofilo