Support

Account

Home Forums General Issues Best way to get Instagram image from oEmbed?

Solved

Best way to get Instagram image from oEmbed?

  • What would be the best way to just get the image from an oEmbed field with an instagram url?

    I tried using preg_match, like this:

    <?php		$instagram = get_field( 'instagram' );
    
    preg_match('/href="(.+?)"/', $instagram, $matches_url );
    $src = $matches_url[1];	
    
    preg_match('&/p(.*)/&', $src, $matches_id );
    $id = $matches_id[1];
    $id = str_replace( str_split( '?/' ), '', $id );
    
    ?>
    
    <img src="https://instagram.com/p/<?php echo $id; ?>/media/?size=l" />

    However, this seems to slow down my site quite a bit. Are there better alternatives for this?

    Thanks!

  • Hi @teun

    You can also get the URL instead of the embedded code by passing the format value to false like this:

    <?php $instagram_url = get_field( 'instagram', false, false ); ?>
    
    <img src="<?php echo $instagram_url; ?>media/?size=l" />

    But the best way to get the image would be by using the API instead. This page should give you more idea about it: https://www.instagram.com/developer/endpoints/media/.

    I hope this helps 🙂

  • I can’t believe it was that easy. Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Best way to get Instagram image from oEmbed?’ is closed to new replies.