Get Instagram User ID From Username, WordPress & PHP


About The Author
Avatar Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







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

Leave a Reply

Your email address will not be published. Required fields are marked *


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!