Enter your keyword

Blog

How to use Instagram API in Wordpress Shortcode

/*  Following funt in theme functions.php file */
add_shortcode('instagram_feeds', 'instagram_feeds_shortcode');
function rudr_instagram_api_curl_connect( $api_url ){
	$connection_c = curl_init(); // initializing
	curl_setopt( $connection_c, CURLOPT_URL, $api_url ); // API URL to connect
	curl_setopt( $connection_c, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
	curl_setopt( $connection_c, CURLOPT_TIMEOUT, 20 );
	$json_return = curl_exec( $connection_c ); // connect and get json data
	curl_close( $connection_c ); // close connection
	return json_decode( $json_return ); // decode and return
}

function instagram_feeds_shortcode( $atts ) {
    extract( shortcode_atts( array(
        
    ), $atts, 'domain' ) );
  
	$outputs = '';
	$instagram_access_token = 'Instagram Access Token';
	$return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/users/self/media/recent?access_token=".$instagram_access_token."&count=4");
	$outputs .='<ul>';
 		foreach ($return->data as $instagrampost) { 
  			$outputs .= '<li> <figure><img src="'. $instagrampost->images->standard_resolution->url.'" /></figure></li>';
		}
	$outputs .='</ul>';
return $outputs;     
}

No Comments

Add your review

Your email address will not be published.

* Feilds are Required.