How to connect Instagram API using wp_remote_get() function in WordPress
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()
.
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.
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 '
' . $instagram_response->data->username . ' ID: ' . $instagram_response->data->id . '
' . $instagram_response->data->full_name . ' ' . $instagram_response->data->bio . '
' . $instagram_response->data->website . '
Media: ' . $instagram_response->data->counts->media . '
Subscribers: ' . $instagram_response->data->counts->followed_by . '
Subscribed: ' . $instagram_response->data->counts->follows . '
';
// Example of error handling, try to set incorrect user ID to catch an error
// 400 Bad Request
} elseif ( $remote_wp['response']['code'] == 400 ) {
echo '' . $remote_wp['response']['message'] . ': ' . $instagram_response->meta->error_message;
}
Here is the Instagram API response example, taken from the official website:

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 '
';
// more parameters here https://www.instagram.com/developer/endpoints/users/#get_users_media_recent
}
} elseif ( $remote_wp['response']['code'] == 400 ) {
echo '' . $remote_wp['response']['message'] . ': ' . $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

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
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.
You’re welcome :)
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!
I’m glad to help! 🙃
Misha –
Just making sure, does this work with the recent changes that Facebook has done?
Cheers.
Hey Dan,
I didn’t test it with the recent changes yet.
Hello Misha
I need your help and consult for my project according to the instagram API connecting wordpress
How can I contact you?
Hey Daniel,
here
Thanks but I tried this and $instagram_response gives me:
The access token changes every time I get one so how does that work? Do you need to create a session with a unique access token each time?
Also I see you reference the tags and locations endpoints but those have been completely removed from the API docs and supposedly don’t work anymore.
Facebook changed much of the access to Instagram’s API last year although I see your plugin still works by hashtag and I’d consider buying it however I’m just worried that Instagram would block my IP or something if they noticed a lot of requests coming from my site?
Hi,
Currently for me a regular instagram token is still working when trying to get last 20 photos of the token owner or profile info but as I know this is going to be shut down too.
Nothing is constant when we talk about Instagram API 🙃
Neither my customers or I faced with the issue when Instagram blocked IP because of my plugin. Maybe because my plugin caches the response for a couple hours.
Thank you so much. I’ve tried so many tutorials but none worked. This was the only one I could get to work.
However there is just one last thing I need some help with. I wish to also display the date the post was posted. I’ve been using $m->created_time to do so, however it outputs the date as 1570317364 – for a post on October 4. I don’t even know what format this is to even begin converting it through PHP.
Thannks
Hi Matt,
Hope it helps:
You can find more examples in the documentation of
date()
php function.