Created
October 28, 2014 13:54
-
-
Save bluvertigo/bbb2d420f28024e5129a to your computer and use it in GitHub Desktop.
Da completare! - Shortcode che restituisce le foto presenti in un album Facebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* GET FACEBOOK GALLERY | |
* | |
* Visualizza n fotografie casuali di un album di facebook | |
* | |
* @param int $album_id ID dell'album di facebook | |
* @param int $pics Numero di fotografie | |
* @param int $expire Validità della cache in secondi | |
*/ | |
// Add Shortcode | |
function get_facebook_gallery( $atts ) { | |
// Attributes | |
extract( shortcode_atts( | |
array( | |
'album_id' => '10152129213606659', | |
'pics' => '3', | |
'expire' => '86400', | |
), $atts ) | |
); | |
// Code | |
// Verifico che il risultato non sia gia in cache | |
if ( false === ( $fb_images = get_transient( 'facebook_images' ) ) ) { | |
// Url delle API | |
$url = 'https://graph.facebook.com/'. $album_id .'/photos/?limit=0'; | |
// Verifico che le API siano online | |
if ( false != ( $json_file = @file_get_contents( $url ) ) ) { | |
// Leggo il json e metto tutto in una variabile | |
$fb_images = json_decode( $json_file, true ); | |
$result = var_dump($fb_images); | |
$fb_images = $fb_images['data']; | |
// Salvo i dati in cache | |
set_transient( 'facebook_images', $fb_images, $expire ); | |
} else { | |
// Se le API non sono raggiungibili ritorno false | |
return false; | |
} | |
} | |
// Randomizzo le immagini | |
shuffle( $fb_images ); | |
$result = ''; | |
// e ne estraggo le prime 3 | |
for ( $i = 0; $i < $pics; $i++ ) { | |
$result .= '<img src="'. $fb_images[$i]['images'][5]['source'] .'">'; | |
} | |
return $result; | |
} | |
add_shortcode( 'facebook_gallery', 'get_facebook_gallery' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment