Skip to content

Instantly share code, notes, and snippets.

@chrisvanpatten
Last active September 18, 2020 14:16
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chrisvanpatten/521647717fc0e33d2533 to your computer and use it in GitHub Desktop.
Save chrisvanpatten/521647717fc0e33d2533 to your computer and use it in GitHub Desktop.
Super-simple way to grab a few Instagram images and cache them w/ WordPress

This is an easy way to integrate a basic Instagram feed into a WordPress site.

Setup

First, register with Instagram's developer program, then register a client. Set the client ID they provide you in line 4.

Second, get your Instagram user ID by running this in your terminal:

curl -X GET https://api.instagram.com/v1/users/search\?q\=USERNAME\&client_id\=CLIENT_ID

Replace USERNAME with the user whose ID you want, and replace CLIENT_ID with the app client ID you got in step 1.

The ID should be the last entry in the returned JSON object's "data" array. Set that on line 5.

Third, output this stuff somehow. Here's what I did:

<?php

$feed = instagram_feed();

if ( $feed ) {
  foreach ( $feed as $data ) {
    echo '<a href="' . $data->link . '"><img src="' . $data->images->thumbnail->url . '"></a>';
  }
}

With that, you're all set!

Notes

The code uses WordPress' transients API to cache results. By default, the transient will expire every hour. Change that to suit your needs.

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

<?php
function instagram_feed( $count = 2 ) {
$client_id = '';
$instagram_id = '';
if ( false === ( $feed = get_transient( 'instagram_feed' ) ) ) {
$url = 'https://api.instagram.com/v1/users/' . $instagram_id . '/media/recent/?count=' . $count . '&client_id=' . $client_id;
$response = wp_remote_get( $url );
$body = json_decode( $response['body'] );
$feed = $body->data;
set_transient( 'instagram_feed', $feed, 1 * 'HOURS_IN_SECONDS' );
}
return $feed;
}
@smarchal
Copy link

smarchal commented Oct 5, 2015

This is great, gave me a good, fast starting point. One tiny issue, the transient expiry won't get set with this code because it is passing the time constant as a string -- HOURS_IN_SECONDS shouldn't have quotes around it. :) Had a big "duh" moment after trying to figure out why the timeout wasn't getting stored in my options table... Cheers & thanks!

@danielgc
Copy link

danielgc commented Oct 9, 2015

It's HOUR_IN_SECONDS (in singular) according to https://codex.wordpress.org/Transients_API. Thx for sharing!

@BenQuirk
Copy link

Thank you for this!

@aijcoa
Copy link

aijcoa commented Mar 14, 2018

Does this still work? I'm getting The access_token provided is invalid.

@LucasSimpsonFKSE
Copy link

LucasSimpsonFKSE commented Sep 14, 2020

I saw that you are really talented and I am sure that you can build anything. I am sure because I saw what are you doing and how you write the code. I see a lot of potential in you and I am sure that you can do any plugin. Can someone build me a plugin? I am ready to pay for it. I want a plugin that would work something like this automatic comments and likes for Instagram. I want to have this plugin on my website. I am using the newest version of WordPress and I would love to have this plugin. Give me a pm when you are ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment