Created
February 4, 2012 17:57
-
-
Save jchristopher/1739212 to your computer and use it in GitHub Desktop.
Our final Image of the Month WordPress Widget
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
<?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; | |
} | |
} |
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
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 );
$image_id = $instance[$this->image_field];
$image_id2 = $instance[$this->image_field];
$image = new WidgetImageField( $this, $image_id );
$image2 = new WidgetImageField( $this, $image_id2 );
$instance["image_id"] = intval( strip_tags( $new_instance["image_id"] ) );
$instance["image_id2"] = intval( strip_tags( $new_instance["image_id2"] ) );