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;
preg_match('&/p(.*)/&', $src, $matches_id );
$id = $matches_id;
$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?
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!