Skip to content

Instantly share code, notes, and snippets.

@alexx855
Created June 1, 2017 01:09
Show Gist options
  • Save alexx855/ceaefd188bb49be6045bcde621543bcc to your computer and use it in GitHub Desktop.
Save alexx855/ceaefd188bb49be6045bcde621543bcc to your computer and use it in GitHub Desktop.
wordpress instragram shortcode
<?php
function get_instagram_data( $url, $javascript_loop = 0, $timeout = 5 ) {
$url = str_replace( "&amp;", "&", urldecode(trim($url)) );
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
if ($response['http_code'] == 301 || $response['http_code'] == 302) {
ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
if ( $headers = get_headers($response['url']) ) {
foreach( $headers as $value ) {
if ( substr( strtolower($value), 0, 9 ) == "location:" )
return get_url( trim( substr( $value, 9, strlen($value) ) ) );
}
}
}
if ( ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) && $javascript_loop < 5) {
return get_url( $value[1], $javascript_loop+1 );
} else {
return array( $content, $response );
}
}
// Add Shortcode
function wattagency_instagram() {
$json_link="https://www.instagram.com/htsresort/media/";
$json = get_instagram_data($json_link);
$obj = json_decode($json[0], true, 512, JSON_BIGINT_AS_STRING);
$output = '';
$items = array_slice($obj['items'], 0, 8);
if($items){
$output .= '<div class="wattagency_instagram-container">';
foreach ($items as $key => $post){
$pic_text=$post['caption']['text'];
$pic_link=$post['link'];
$pic_like_count=$post['likes']['count'];
$pic_comment_count=$post['comments']['count'];
// $pic_src=str_replace("http://", "https://", $post['images']['thumbnail']['url']);
$pic_src= $post['images']['thumbnail']['url'];
$pic_created_time=date("F j, Y", $post['caption']['created_time']);
$pic_created_time=date("F j, Y", strtotime($pic_created_time . " +1 days"));
$output .= '<div class="wattagency_instagram-column">';
$output .= '<div class="wattagency_instagram-image">';
$output .= '<a href="'.$pic_link.'" target="_blank" style="background-image: url('.$pic_src.')">'.$pic_text.'</a>';
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment