How to Get a List of Contributors from a Github Project

Underscores.me Contributors

We launched underscores.me a few hours ago and the overall feedback is great. People seem to love how we automatically pull in contributors using the GitHub API, so I decided to share the code snippet that does that (using WordPress):

function underscoresme_get_contributors() {
    $transient_key = 'underscoresme_contributors';
    $contributors = get_transient( $transient_key );
    if ( false !== $contributors )
        return $contributors;

    $response = wp_remote_get( 'https://api.github.com/repos/Automattic/_s/contributors' );
    if ( is_wp_error( $response ) )
        return array();

    $contributors = json_decode( wp_remote_retrieve_body( $response ) );
    if ( ! is_array( $contributors ) )
        return array();

    set_transient( $transient_key, $contributors, 3600 );
    return (array) $contributors;
}

This function returns an array of contributors to the underscores project (or wherever that URL points to) on GitHub. We cache the results in a transient for better performance too! On the front-end of your site, use the function to grab the array of contributors and print_r to find out what data is available. Here’s an example that prints out the contributors logins:

foreach ( underscoresme_get_contributors() as $contributor ) {
    echo $contributor->login;
}

Visit the GitHub API docs to learn more.

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

4 comments