Skip to content

Instantly share code, notes, and snippets.

@NateJacobs
Created September 20, 2012 15:44
Show Gist options
  • Save NateJacobs/3756702 to your computer and use it in GitHub Desktop.
Save NateJacobs/3756702 to your computer and use it in GitHub Desktop.
Cactus Brick Flickr Widget
<?php
/*
Plugin Name: Cactus Brick Flickr Widget
Plugin URI: http://cactusbrick.org
Description: Grab photos from Flickr
Version: 1.0
Author: Nate Jacobs
Author URI: http://natejacobs.org
License: GPLv2 or later
*/
add_action( 'widgets_init', 'CactusBrickFlickrWidget' );
function CactusBrickFlickrWidget()
{
register_widget( 'CactusBrickFlickrWidget' );
}
/**
* Flickr Photos Widget
*
* Creates a widget that shows the recent Flickr photos by a user or group
*
* @author Nate Jacobs
* @since 1.0
*/
class CactusBrickFlickrWidget extends WP_Widget
{
/**
* Plugin Load
*
* Generate options for the widget display in the admin dashboard.
*
* @author Nate Jacobs
* @since 1.0
*/
public function __construct()
{
$options = array( 'description' => __( 'Display photos from Flickr.com by a user or group.' ), 'classname' => 'cactus_brick_flickr' );
parent::__construct( 'cactus_brick_flickr_widget', $name = __( 'Cactus Brick Flickr' ), $options );
}
/**
* Create Widget Form
*
* Create the necessary form to customize the widget.
* Use http://idgettr.com to get the ID for a group or user
*
* @uses title @since 1.0
*
* @author Nate Jacobs
* @since 1.0
*/
public function form( $instance )
{
$instance = wp_parse_args( ( array ) $instance, array( 'title' => __( 'Cactus Brick Flickr' ), 'flickr_id' => __( ' ' ) ) );
$title = esc_attr( $instance['title'] );
$flickr_id = esc_attr( $instance['flickr_id'] );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'flickr_id' ); ?>">Flickr ID:</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'flickr_id' ); ?>" name="<?php echo $this->get_field_name( 'flickr_id' ); ?>" type="text" value="<?php echo $flickr_id; ?>">
</p>
<?php
}
/**
* Update Widget Options
*
* Updates any options used to customize the widget.
*
* @author Nate Jacobs
* @since 1.0
*/
public function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['flickr_id'] = strip_tags( $new_instance['flickr_id'] );
return $instance;
}
/**
* Create Cactus Brick Flickr Widget
*
* Generates the widget that displays the photos from a user or group.
*
* @author Nate Jacobs
* @since 1.0
*/
public function widget( $args, $instance )
{
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );
$flickr_id = $instance['flickr_id'];
$result = wp_remote_get( "http://api.flickr.com/services/feeds/groups_pool.gne?id=$flickr_id&format=php_serial", array( 'method' => 'GET' ) );
$this->httpcode = $result['response']['code'];
$photos = unserialize( $result['body'] );
echo $before_widget;
if ( $title )
echo $before_title . '<a href="http://www.flickr.com/groups/cactusbrick/pool/">'. $title .'</a>' . $after_title;
try {
if ( $this->httpcode == '200' )
{
foreach ( $photos['items'] as $photo )
{
echo "<a href=$photo[url]><img class='flickr-img' src=$photo[thumb_url]></a>";
}
}
else
{
$error = 'There are no photos to display';
throw new Exception($error);
}
}
catch ( Exception $e )
{
echo $e->getMessage();
}
echo $after_widget;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment