WordPress: display your GitHub activity

Every GitHub user is provided with a public Atom or JSON feed which contains all his recent activity. This feed is located at https://github.com/username.atom or https://github.com/username.json. The problem with WordPress is that the built-in fetch_feed() function cannot handle the SSL connection used by GitHub. For that reason, we should use cURL and SimpleXML. Let's see how.

We can create the following utility function in our functions.php file:

function display_github_activity($url = 'https://github.com/username.atom', $maxitems = 5) {

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	$atom = curl_exec($ch);
	curl_close($ch);
	
	
	$feed = simplexml_load_string($atom);
	$entries = $feed->entry;
	$html = '<ul>' . "\n";
	$max = $maxitems - 1;
	$i = -1;
	
	do {
	
	    $i++;
	
		$title = strip_tags($entries[$i]->title);
		$link = $entries[$i]->link['href'];
		$content = strip_tags($entries[$i]->content);
		$date = $entries[$i]->published;
		$date_time = strftime('%Y-%m-%d %H:%M:%S', strtotime($date));
		
		$html .= '<li><small>' . $date_time . '</small>' . "\n" . '<h4><a href="' . $link . '">' . $title . '</a></h4>' .
		          "\n". 
		          '<p>' . $content . '</p></li>' . "\n";	
	
	
	} while($i < $max);
	
	$html .= '</ul>' . "\n";
	
	
	return $html;
	    


}

Then you can use the above function in your theme:

<?php echo display_github_activity('https://github.com/gabrieleromanato.atom', 3); ?>

Which outputs:

[caption id="attachment_1932" align="aligncenter" width="500" caption="Your GitHub activity displayed on WordPress"]Your GitHub activity displayed on WordPress[/caption]
Prev Articles Next