How to connect Instagram API using wp_remote_get() function in WordPress

In these examples I will show you how to get user info from Instagram or his recent media. All the examples work in Sandbox Mode.

#httpapi, #Instagram  /   7

Everytime when I worked with Instagram API I used either jQuery ajax or cURL. But now for each GET call to Instagram API I use wp_remote_get() WordPress function.

So, you may go to API endpoints page and if you see GET near the endpoint it means that it can be implemented via wp_remote_get().

Instagram API endpoints. All GET-endpoints can be called via wp_remote_get() WP function.

For POST-endpoints you could use wp_remote_post(), but my app is in Sandbox Mode, so I have no chance to test it and I don’t mention untested examples in my posts.

Now let’s look at some examples that can be easily implemented for Sandbox Mode apps.

Use this tool to get your Access Token.

Example 1. Get information about the Instagram user via wp_remote_get()

You do not have to approve your app to get profile information of an access token owner. And it is really good news because many companies and people asks us, the developers to display the number of their Instagram subscribers.

Below is the example of getting ALL the user information from Instagram.

$your_token = 'Access token is required, read above how to obtain it';
// if your app is not approved - always use 'self'
$ig_user_id = 'self';
// Instagram API connection
$remote_wp = wp_remote_get( "" . $ig_user_id . "/?access_token=" . $your_token );
// Instagram response is JSON encoded, let's convert it to an object
$instagram_response = json_decode( $remote_wp['body'] );
// 200 OK
if( $remote_wp['response']['code'] == 200 ) {
	// $instagram_response->data object contains all the user information
	echo '
		<img src="' . $instagram_response->data->profile_picture . '" />
		<h3>' . $instagram_response->data->username . ' <em>ID: ' . $instagram_response->data->id . '</em></h3>
		<p><strong>' . $instagram_response->data->full_name . '</strong> ' . $instagram_response->data->bio . '</p>
		<a href="' . $instagram_response->data->website . '">' . $instagram_response->data->website . '</a>
	<div id="counts">
		<span>Media: ' . $instagram_response->data->counts->media . '</span>
		<span>Subscribers: ' . $instagram_response->data->counts->followed_by . '</span>
		<span>Subscribed: ' . $instagram_response->data->counts->follows . '</span>
// Example of error handling, try to set incorrect user ID to catch an error
// 400 Bad Request
} elseif ( $remote_wp['response']['code'] == 400 ) {
	echo '<b>' . $remote_wp['response']['message'] . ': </b>' . $instagram_response->meta->error_message;

Here is the Instagram API response example, taken from the official website:

Instagram API response example

Example 2. Use wp_remote_get() function to fetch the recent media of a user

This example may look even more simple than the previous one.

Yes, the Sandbox Mode allows you to get your own latest media (to say exactly — 20 recent media of the access token owner).

$your_token = 'YOUR ACCESS TOKEN HERE'; // read above how to get it
// I recommend to use "self" if your application is not approved
$ig_user_id = 'self';
$remote_wp = wp_remote_get( "" . $ig_user_id . "/media/recent/?access_token=" . $your_token );
$instagram_response = json_decode( $remote_wp['body'] );
if( $remote_wp['response']['code'] == 200 ) {
	foreach( $instagram_response->data as $m ) {
		echo '<a href="' . $m->link . '" id="media-' . $m->id . '" class="type-' . $m->type . '">
			<img src="' . $m->images->standard_resolution->url . '" title="' . $m->caption->text . '" width="' . $m->images->standard_resolution->width . '" height="' . $m->images->standard_resolution->height . '" />
		// more parameters here
} elseif ( $remote_wp['response']['code'] == 400 ) {
	echo '<b>' . $remote_wp['response']['message'] . ': </b>' . $instagram_response->meta->error_message;

Is there a chance to get media by tags or by location?

Actually yes, but if your application is in Sandbox Mode, your results will be restricted by the 20 recent media of the token owner. So, only if there are tagged media among those 20 items, they will be displayed.

The same mechanism applies for getting photos/videos by a location.

If your app is not approved and you want to avoid these restrictions, please try my plugin. It doesn’t require an Access Token at all.

But how to get media by tags or by location if your application is approved?

It is easy enough — you could use the code from Example 2, just change the API endpoint for tags to:

/tags/{TAG NAME HERE}?access_token=ACCESS-TOKEN

And for locations to:

/locations/{LOCATION ID HERE}/media/recent?access_token=ACCESS-TOKEN

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 7

  • This was perfect. Exactly what I needed as the way I was getting media before (scraping) was not working anymore in my slider. It wasn’t getting the recent images.

    I was shocked that it worked the first time, using the slider and everything. All I had to do was change the key and the code worked straight off. Thanks! You saved me both time and money.

  • Hey Misha,

    This is awesome, you’ve been a huge help to me in trying to build an IG gallery that is sortable by tag.

    I’ve noticed that the /tags/ API call seems to return results that contain my search term but aren’t an exact match and I was wondering if you’ve run into the same problem. For example, one of the hashtags I’m requesting is ‘weddinginvitations’ and it seems to return one result that’s tagged with ‘weddinginspiration’, but does not have the tag ‘weddinginvitations’.

    Seems odd to me that it wouldn’t return exact matches only. Curious if you’ve run into this issue as well?

    • Here to answer my own question in case anyone else runs into this!

      It looks like sometimes, but not always, the API response’s data->tags array will only include hashtags made in the initial user comment.

      Basically, sometimes a user will come back and comment a second time on their own image and include more hashtags, but it looks like that data doesn’t always make it into the API’s response.

      Very odd….

    • Hello Brendan,

      Thanks for sharing!

  • Thanks Misha – this article was exactly what I was looking for. Bloody life saver!

Leave your question or feedback

Please, enter a comment
Please, enter a name
Incorrect email