Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@zamartz 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

This comment has been minimized.

Copy link
Owner Author

@jchristopher jchristopher commented Feb 26, 2013

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

This comment has been minimized.

Copy link

@zamartz 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

This comment has been minimized.

Copy link

@zamartz 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

This comment has been minimized.

Copy link

@stephencranedesign stephencranedesign commented Jun 7, 2013

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