Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created February 4, 2012 17:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jchristopher/1739212 to your computer and use it in GitHub Desktop.
Save jchristopher/1739212 to your computer and use it in GitHub Desktop.
Our final Image of the Month WordPress Widget
<?php
// we can only use this Widget if the plugin is active
if( class_exists( 'WidgetImageField' ) )
add_action( 'widgets_init', create_function( '', "register_widget( 'ITI_Widget_Image_OTM' );" ) );
class ITI_Widget_Image_OTM extends WP_Widget
{
var $image_field = 'image'; // the image field ID
function __construct()
{
$widget_ops = array(
'classname' => 'image_otm',
'description' => __( "Image of the Month")
);
parent::__construct( 'image_otm', __('Image of the Month'), $widget_ops );
}
function form( $instance )
{
$headline = esc_attr( isset( $instance['headline'] ) ? $instance['headline'] : '' );
$image_id = esc_attr( isset( $instance[$this->image_field] ) ? $instance[$this->image_field] : 0 );
$blurb = esc_attr( isset( $instance['blurb'] ) ? $instance['blurb'] : '' );
$image = new WidgetImageField( $this, $image_id );
?>
<p>
<label for="<?php echo $this->get_field_id( 'headline' ); ?>"><?php _e( 'Headline:' ); ?>
<input class="widefat" id="<?php echo $this->get_field_id( 'headline' ); ?>" name="<?php echo $this->get_field_name( 'headline' ); ?>" type="text" value="<?php echo $headline; ?>" />
</label>
</p>
<div>
<label><?php _e( 'Image:' ); ?></label>
<?php echo $image->get_widget_field(); ?>
</div>
<p>
<label for="<?php echo $this->get_field_id( 'blurb' ); ?>"><?php _e( 'Blurb:' ); ?>
<input class="widefat" id="<?php echo $this->get_field_id( 'blurb' ); ?>" name="<?php echo $this->get_field_name( 'blurb' ); ?>" type="text" value="<?php echo $blurb; ?>" />
</label>
</p>
<?php
}
function widget( $args, $instance )
{
extract($args);
$headline = $instance['headline'];
$image_id = $instance[$this->image_field];
$blurb = $instance['blurb'];
$image = new WidgetImageField( $this, $image_id );
echo $before_widget;
?>
<?php if( !empty( $headline ) ) : ?>
<h5 class="branded"><?php echo $headline; ?></h5>
<?php endif; ?>
<?php if( !empty( $image_id ) ) : ?>
<img src="<?php echo $image->get_image_src( 'thumbnail' ); ?>" width="<?php echo $image->get_image_width( 'thumbnail' ); ?>" height="<?php echo $image->get_image_height( 'thumbnail' ); ?>" />
<?php endif; ?>
<?php if( !empty( $blurb ) ) : ?>
<p><?php echo $blurb; ?></p>
<?php endif; ?>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['headline'] = strip_tags( $new_instance['headline'] );
$instance[$this->image_field] = intval( strip_tags( $new_instance[$this->image_field] ) );
$instance['blurb'] = strip_tags( $new_instance['blurb'] );
return $instance;
}
}
@zamartz
Copy link

zamartz commented Feb 25, 2013

How would you modify this to use 2 image fields in one widget?
Specifically updating... with two different images.

@jchristopher
Copy link
Author

Just duplicate lines having to do with the image field and give it a unique name/id.

Lines 24, 27, 34-37, 51, 54, 62-64, and 78

@zamartz
Copy link

zamartz commented Feb 26, 2013

line 78 is the area that i dont understand - what would be an example of that being image1 and image2 ?

@zamartz
Copy link

zamartz commented Mar 26, 2013

Is this correct for a duplicate of each line? I cannot seam to find what I am missing

$image_id = esc_attr( isset( $instance[$this->image_field] ) ? $instance[$this->image_field] : 0 );
$image_id2 = esc_attr( isset( $instance[$this->image_field] ) ? $instance[$this->image_field] : 0 ;

$image = new WidgetImageField( $this, $image_id );
$image2 = new WidgetImageField( $this, $image_id2 );

get_widget_field(); ?>
get_widget_field(); ?>

$image_id = $instance[$this->image_field];
$image_id2 = $instance[$this->image_field];

$image = new WidgetImageField( $this, $image_id );
$image2 = new WidgetImageField( $this, $image_id2 );

            <img src="<?php echo $image->get_image_src( 'thumbnail' ); ?>" width="<?php echo $image->get_image_width( 'thumbnail' ); ?>" height="<?php echo $image->get_image_height( 'thumbnail' ); ?>" />
        <?php endif; ?>
            <img src="<?php echo $image2->get_image_src( 'thumbnail' ); ?>" width="<?php echo $image2->get_image_width( 'thumbnail' ); ?>" height="<?php echo $image2->get_image_height( 'thumbnail' ); ?>" />
        <?php endif; ?>

$instance["image_id"] = intval( strip_tags( $new_instance["image_id"] ) );
$instance["image_id2"] = intval( strip_tags( $new_instance["image_id2"] ) );

@stephencranedesign
Copy link

I've been struggling with this too. Did you ever figure it out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment