Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created December 14, 2015 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/ff23ada3e309b8b1719e to your computer and use it in GitHub Desktop.
Save tommcfarlin/ff23ada3e309b8b1719e to your computer and use it in GitHub Desktop.
[WordPress] Retrieve Flickr Photos in WordPress
<?php
/**
* Defines and wrap an instance of the cURL library to retrieve information from
* the specified URL.
*
* @param string $string The URL from which to retrieve information.
* @return $data The data retrieved from the incoming URL.
*/
function acme_curl( $url ) {
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_USERAGENT, '' );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
$data = curl_exec($ch);
if ( 0 !== curl_errno( $ch ) || 200 !== curl_getinfo($ch, CURLINFO_HTTP_CODE ) ) {
$data = FALSE;
}
curl_close( $ch );
return $data;
}
<?php
/**
* @return string $user_id The ID of the user that corresponds to the username specified in the customizer.
*/
function acme_get_flickr_user_id() {
$flickr_username = get_theme_mod( 'acme_flickr_username' );
$response = acme_curl( 'https://api.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=YOUR_API_KEY&username=' . $flickr_username );
$oXML = simplexml_load_string( $response );
$user_id = $oXML->user['id'];
echo $user_id;
}
<?php
/**
* Renders the most recent photos from the specified Flickr account.
* in `index.php`.
*
* @package Acme
*/
?>
<div id="flickr">
<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=10&amp;display=latest&amp;size=s&amp;layout=x&amp;source=user&amp;user=<?php echo acme_get_flickr_user_id(); ?>"></script>
</div><!-- #flickr -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment