Get Instagram User ID From Username, WordPress & PHP
Get Instagram User ID From Username, WordPress & PHP
Since we’ve been on the Instagram train lately, I decided to post about a recent snippet I needed to grab a users Instagram numerical ID from their username. When writing custom Instagram implementations utilizing InstafeedJS, the Instagram numerical ID is needed rather than the traditional username. Luckily, once you have an APP authorization token, this information is fairly easy to grab. To start, add the following function to your themes functions.php file:
function getInstaID($username) { $username = strtolower($username); // sanitization $token = "yourinstagramapiaccesstoken"; $url = "https://api.instagram.com/v1/users/search?q=".$username."&access_token=".$token; $get = file_get_contents($url); $json = json_decode($get); foreach($json->data as $user) { if($user->username == $username) { return $user->id; } } return '00000000'; // return this if nothing is found }
To output the results of the function, simply use:
echo getInstaID('instagramusername');
We can now use that user ID within a custom InstafeedJS function to return posts from a specific user.
Share Your Thoughts