Skip to content

Instantly share code, notes, and snippets.

@zoerooney
Last active August 29, 2015 14:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zoerooney/300fe1a302670e88109e to your computer and use it in GitHub Desktop.
Save zoerooney/300fe1a302670e88109e to your computer and use it in GitHub Desktop.
<div id="instagram" class="widget">
<?php
// Set variables
$image_number = 2;
$image_size = 'low_resolution'; //options: low_resolution (306), thumbnail (150), standard_resolution (612)
// Create the API URL for the user data
$user_access_token = '123456.xxxxxxxx.zzzzzzzzzzzzzzzzzzzzzzz';
$user_data_url = 'https://api.instagram.com/v1/users/self/?access_token=' . $user_access_token;
// Use a transient for the feed URL (performance boost)
if ( false === ( $user_data = get_transient( 'instagram_user_data' ) ) ) {
$user_data = @file_get_contents( $user_data_url );
// Put the results in a transient. Expire after 4 hours.
set_transient( 'instagram_user_data', $user_data, 4 * HOUR_IN_SECONDS );
}
// Decode the JSON in the file
$user_stats = json_decode( $user_data, true );
$user_image_count = $user_stats['data']['counts']['media'];
$user_followers_count = $user_stats['data']['counts']['followed_by'];
$user_follows_count = $user_stats['data']['counts']['follows'];
// Create the API URL for the user feed
$user_feed_url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' . $user_access_token . '&COUNT=' . $image_number;
// Use a transient for the feed URL (performance boost)
if ( false === ( $user_feed = get_transient( 'instagram_user_feed' ) ) ) {
$user_feed = @file_get_contents( $user_feed_url );
// Put the results in a transient. Expire after 4 hours.
set_transient( 'instagram_user_feed', $user_feed, 4 * HOUR_IN_SECONDS );
}
// Decode the JSON in the file
$user_images = json_decode( $user_feed, true );
?>
<div class="widget-content">
<h3 class="widget-title">FOLLOW ALONG ON INSTAGRAM</h3>
<div id="instagram-feed">
<?php $image = 0;
// Loop through images in the feed
for ( $user_image = 0; $user_image < $image_number; $user_image++ ) {
$user_image_data = $user_images['data'][$image];
// Display the images:
$user_link = $user_image_data['link'];
$user_caption = $user_image_data['caption']['text'];
$user_image_url = $user_image_data['images'][$image_size]['url'];
?>
<div class="instagram-image">
<a href="<?php echo $user_link; ?>" target="_blank"><img src="<?php echo $user_image_url; ?>" alt="<?php echo $user_caption; ?>" /></a>
</div>
<?php
$image++;
} ?>
</div>
<div class="instagram-info">
<a href="http://instagram.com/username" target="_blank">@username</a><br>
<?php echo $user_followers_count; ?> followers<br>
<?php echo $user_image_count; ?> posts<br>
<?php echo $user_follows_count; ?> following
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment