Misha Rudrastyh

Misha Rudrastyh

WordPress Developer

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.

June 28, 2016 Leave comment

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.

Get 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( "https://api.instagram.com/v1/users/" . $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( "https://api.instagram.com/v1/users/" . $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 https://www.instagram.com/developer/endpoints/users/#get_users_media_recent
} 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 the Access Token at all.
Download WordPress plugin

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 new posts about WordPress

once a week

Leave comment

Please, enter a comment
Please, enter a name
Incorrect email
© 2016 Misha Rudrastyh
Developed on with