Skip to content

Instantly share code, notes, and snippets.

@Vinsanity
Created April 22, 2016 15:19
Show Gist options
  • Save Vinsanity/1f91405a6ceff4cf36fb5514b85d01b2 to your computer and use it in GitHub Desktop.
Save Vinsanity/1f91405a6ceff4cf36fb5514b85d01b2 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Epicureangram
* Plugin URI: http://3five.com/
* Description: Epicurean Instragram shortcode plugin.
* Version: 1.0
* Author: Vincent Listrani
* Author URI: http://3five.com/
*/
// Get the Instagram Feed.
/**
* Instagram Feed storage and parsing
*
* @return string
*/
function get_epicureangram_feed() {
// Get or set transient for speed and not exceeding the Instagram API request limits.
if ( false === ( $result = get_transient( 'epicurean_cached_instagram_feed' ) ) ) {
$result = wp_remote_get( 'https://api.instagram.com/v1/users/self/media/recent?access_token=498791840.8e30d44.353619e1a5524d3dbae0954e01366cd2&count=5' );
set_transient( 'epicurean_cached_instagram_feed', $result, 60 * 60 * 4 );
}
// Set output variable.
$output = '';
if ( is_wp_error( $result ) ) {
$error_message = $result->get_error_message();
$output = "Something went wrong: $error_message";
} else {
// Process the $result json.
$result = json_decode( $result['body'] );
$data_object = array();
$i = 0;
// Get username and image.
foreach ( $result->data as $item ) {
$data_object[ $i ]['plink'] = $item->link;
$data_object[ $i ]['user'] = $item->user->username;
$data_object[ $i ]['image_src'] = $item->images->standard_resolution->url;
$i++;
}
// Loop through and build out the template for each image.
foreach ( $data_object as $data ) {
$output .= '<a target="_blank" href="'.$data['plink'].'" class="instagram-feed-item"><img src="'.$data['image_src'].'" alt="'.$data['user'].' pictures" class="instagram-feed-image"></a> ';
}
}
// Return the output string.
return $output;
}
// Register the shortcode.
add_shortcode( 'epicureangram', 'epicureangram_embed_shortcode' );
// Declare the shortcode.
/**
* Epicurean Instagram Feed Shortcode Function
*
* @param array $atts Add an array of attributes to be used as params within the shortcode.
* @param null $content The content to output.
*/
function epicureangram_embed_shortcode( $atts, $content = null ) {
get_epicureangram_feed();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment