Last active
December 15, 2015 04:09
-
-
Save lswilson/5199853 to your computer and use it in GitHub Desktop.
WordPress: Image upload in a widget
This file contains 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 | |
/** | |
* NGData Widget Icons | |
* | |
* @author Lisa Sabin-Wilson | |
* @since 1.0 | |
*/ | |
class icon_text extends WP_Widget | |
{ | |
function icon_text() { | |
$widget_ops = array( | |
'classname' => 'icon-text group', | |
'description' => __( 'Arbitrary text or HTML, with a simple icon near title.', 'ngdata' ) | |
); | |
$control_ops = array( 'id_base' => 'icon-text', 'width' => 430 ); | |
$this->WP_Widget( 'icon-text', 'NGDATA Icon Text Widget', $widget_ops, $control_ops ); | |
wp_enqueue_style( 'thickbox' ); | |
wp_enqueue_script( 'thickbox' ); | |
//wp_enqueue_script( 'media-upload' ); | |
add_action( 'admin_print_footer_scripts', array( &$this, 'add_script_textimage' ), 999 ); | |
} | |
function form( $instance ) { | |
/* Impostazioni di default del widget */ | |
$defaults = array( | |
'title' => '', | |
'text' => '', | |
'img_url' => '', | |
'widget_url' => '' | |
); | |
$instance = wp_parse_args( (array) $instance, $defaults ); ?> | |
<p> | |
<label> | |
<strong><?php _e( 'Title', 'ngdata' ) ?>:</strong><br /> | |
<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" /> | |
</label> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id('img_url'); ?>">Image</label><br /> | |
<input type="text" class="img" name="<?php echo $this->get_field_name('img_url'); ?>" id="<?php echo $this->get_field_id('img_url'); ?>" value="<?php echo $instance['img_url']; ?>" /> | |
<input type="button" class="select-img" value="Select Image" /> | |
</p> | |
<p> | |
<label> | |
<textarea class="widefat" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" cols="20" rows="5"><?php echo $instance['text']; ?></textarea> | |
</label> | |
</p> | |
<p> | |
<label> | |
<strong><?php _e( 'URL', 'ngdata' ) ?>:</strong><br /> | |
<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'widget_url' ); ?>" name="<?php echo $this->get_field_name( 'widget_url' ); ?>" value="<?php echo $instance['widget_url']; ?>" /> | |
</label> | |
</p> | |
<?php | |
} | |
function widget( $args, $instance ) { | |
extract( $args ); | |
$title = apply_filters( 'widget_title', $instance['title'] ); | |
$widget_url = $instance['widget_url']; | |
$img_url = $instance['img_url']; | |
echo $before_widget; | |
?> | |
<a href="<?php echo $widget_url ?>"> | |
<img src="<?php echo $img_url ?>" alt="" /> | |
<h3><?php echo $title; ?></h3> | |
<p><?php echo do_shortcode( $instance['text'] ) ?></p> | |
</a> | |
<?php echo $after_widget; | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags( $new_instance['title'] ); | |
$instance['img_url'] = $new_instance['img_url']; | |
$instance['text'] = $new_instance['text']; | |
$instance['widget_url'] = $new_instance['widget_url']; | |
return $instance; | |
} | |
function add_script_textimage() { | |
?> | |
<script type="text/javascript"> | |
var image_field; | |
jQuery(function($){ | |
$(document).on('click', 'input.select-img', function(evt){ | |
image_field = $(this).siblings('.img'); | |
tb_show('', 'media-upload.php?type=image&TB_iframe=true'); | |
return false; | |
}); | |
window.send_to_editor = function(html) { | |
image_field.val($(html).attr('src')); | |
tb_remove(); | |
} | |
}); | |
</script> | |
<?php | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment