Skip to content

Instantly share code, notes, and snippets.

@levantoan
Last active May 26, 2016 04:18
Show Gist options
  • Save levantoan/c6646daa0ada1c1f5d9ac151593628b4 to your computer and use it in GitHub Desktop.
Save levantoan/c6646daa0ada1c1f5d9ac151593628b4 to your computer and use it in GitHub Desktop.
Shortcode hiển thị tối đa 12 ảnh mới nhất trong instagram cho user
<?php
/*
Hiển thị tối đa 12 ảnh mới nhất trong instagram cho user
Cách dùng: [instagram_widget user="_Your User Here_" show="_Your Number show (max 12)_"]
by www.levantoan.com
*/
function scrape_instagram($username, $slice = 9) {
if (false === ($instagram = get_transient('instagram-photos-'.sanitize_title_with_dashes($username)))) {
$remote = wp_remote_get('http://instagram.com/'.trim($username));
if (is_wp_error($remote))
return new WP_Error('site_down', __('Unable to communicate with Instagram.', 'devvn'));
if ( 200 != wp_remote_retrieve_response_code( $remote ) )
return new WP_Error('invalid_response', __('Instagram did not return a 200.', 'devvn'));
$shards = explode('window._sharedData = ', $remote['body']);
$insta_json = explode(';</script>', $shards[1]);
$insta_array = json_decode($insta_json[0], TRUE);
if (!$insta_array)
return new WP_Error('bad_json', __('Instagram has returned invalid data.', 'devvn'));
$images = $insta_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'];
$usercheck = $insta_array['entry_data']['ProfilePage'][0]['user']['username'];
$instagram = array();
foreach ($images as $image) {
if (!($image['is_video']) && $usercheck == $username) {
$instagram[] = array(
'code' => $image['code'],
'description' => $image['caption'],
'link' => 'https://www.instagram.com/p/'.$image['code'],
'time' => $image['date'],
'comments' => $image['comments']['count'],
'likes' => $image['likes']['count'],
'thumbnail' => $image['thumbnail_src'],
'large' => $image['display_src']
);
}
}
$instagram = base64_encode( serialize( $instagram ) );
set_transient('instagram-photos-'.sanitize_title_with_dashes($username), $instagram, apply_filters('null_instagram_cache_time', HOUR_IN_SECONDS*2));
}
$instagram = unserialize( base64_decode( $instagram ) );
return array_slice($instagram, 0, $slice);
}
add_shortcode('instagram_widget', 'instagram_widget_func');
function instagram_widget_func($atts){
extract(shortcode_atts(array(
'user' => 'vickyheiler',
'show' => 20
), $atts));
if(!function_exists('scrape_instagram')) return;
$listInstagram = scrape_instagram($user,$show);
if ( is_wp_error( $listInstagram ) ) {
echo $listInstagram->get_error_message();
return;
}
if(!is_array($listInstagram) || empty($listInstagram)) return;
?>
<div class="instagram_title"><a href="https://www.instagram.com/<?=sanitize_title_with_dashes($user)?>">INSTAGRAM @<?=sanitize_title_with_dashes($user)?></a></div>
<div class="instagram_list">
<?php foreach ($listInstagram as $image){?>
<div class="instagram_list_box"><a href="<?=esc_url($image['link'])?>" target="_blank"><img src="<?=$image['thumbnail']?>"></a></div>
<?php }?>
</div>
<?php
}
.instagram_list {
position: relative;
}
.instagram_list::after {
content: "";
display: table;
clear: both;
}
.instagram_list_box {
width: 10%;
float: left;
display: block;
}
.instagram_list_box img {
display: block;
width: 100%;
height: auto;
}
.instagram_list_box a {
display: block;
padding: 2px;
}
.instagram_title {
display: block;
text-align: center;
text-transform: uppercase;
position: relative;
}
.instagram_title a {
display: inline-block;
color: #333;
padding: 10px 15px;
z-index: 2;
background: #fff;
position: relative;
}
.instagram_title a:hover,
.instagram_list_box img:hover {
filter: alpha(opacity=85);
opacity: .85;
}
.instagram_title::before {
content: "";
position: absolute;
top: 50%;
height: 1px;
width: 100%;
background: #ccc;
left: 0;
z-index: 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment