30 Mar Shortcode instagram pour WordPress
<?php
/*
Vous pouvez mettre ce code dans le fichier functions.php du votre thème
*/
function insShortcode() {
$remote = wp_remote_get('https://www.instagram.com/' . trim('USERNAME'));
if (is_wp_error($remote))
return new WP_Error('site_down', esc_html__('Unable to communicate with Instagram.'));
if (200 != wp_remote_retrieve_response_code($remote))
return new WP_Error('invalid_response', esc_html__('Instagram did not return a 200.'));
$shards = explode('window._sharedData = ', $remote['body']);
$insta_json = explode(';</script>', $shards[1]);
$insta_array = json_decode($insta_json[0], TRUE);
$limit = 10;
$image_array= array();
?>
<ul>
<?php
for ($i=0; $i < $limit; $i++) {
if(isset($insta_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i])){
$latest_array = $insta_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i]['node'];
$image_data = $latest_array['thumbnail_src'];
array_push($image_array, $image_data);
}
}
foreach ($image_array as $image) { ?>
<li>
<a href="<?= $image; ?>" target="_blank" title="" class="">
<img src="<?= $image; ?>" alt="image instagram" />
<span class="like-count">
<i class="ion-heart"></i>
</span>
</a>
</li>
<?php } ?>
</ul>
<?php }
add_shortcode('InstagramShortcode', 'insShortcode'); ?>
Utilisation dans le site
<?= do_shortcode('[InstagramShortcode]'); ?>