Skip to content

Instantly share code, notes, and snippets.

@rodica-andronache
Last active July 6, 2016 10:03
Show Gist options
  • Select an option

  • Save rodica-andronache/b84bee82b1ab8ab415c9 to your computer and use it in GitHub Desktop.

Select an option

Save rodica-andronache/b84bee82b1ab8ab415c9 to your computer and use it in GitHub Desktop.
widget cu text si imagine
add_action('widgets_init', 'zerif_ourfocus_widget');
function zerif_ourfocus_widget() {
register_widget( 'zerif_ourfocus' );
}
add_action('admin_enqueue_scripts', 'zerif_ourfocus_widget_scripts');
function zerif_ourfocus_widget_scripts() {
wp_enqueue_media();
wp_enqueue_script('zerif_our_focus_widget_script', get_template_directory_uri() . '/js/widget.js', false, '1.0', true);
}
class zerif_ourfocus extends WP_Widget {
function zerif_ourfocus() {
$widget_ops = array('classname' => 'ctUp-ads');
$this->WP_Widget('ctUp-ads-widget', 'Zerif - Our focus widget', $widget_ops);
}
function widget($args, $instance) {
extract($args);
echo $before_widget;
?>
<h1><?php echo apply_filters('widget_title', $instance['text'] ); ?></h1>
<a href="#">
<img src="<?php echo esc_url($instance['image_uri']); ?>" />
</a>
<?php
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['text'] = strip_tags( $new_instance['text'] );
$instance['image_uri'] = strip_tags( $new_instance['image_uri'] );
return $instance;
}
function form($instance) {
?>
<p>
<label for="<?php echo $this->get_field_id('text'); ?>">Text</label><br />
<input type="text" name="<?php echo $this->get_field_name('text'); ?>" id="<?php echo $this->get_field_id('text'); ?>" value="<?php echo $instance['text']; ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('image_uri'); ?>">Image</label><br />
<?php
if ( $instance['image_uri'] != '' ) :
echo '<img class="custom_media_image" src="' . $instance['image_uri'] . '" style="margin:0;padding:0;max-width:100px;float:left;display:inline-block" /><br />';
endif;
?>
<input type="text" class="widefat custom_media_url" name="<?php echo $this->get_field_name('image_uri'); ?>" id="<?php echo $this->get_field_id('image_uri'); ?>" value="<?php echo $instance['image_uri']; ?>" style="margin-top:5px;">
<input type="button" class="button button-primary custom_media_button" id="custom_media_button" name="<?php echo $this->get_field_name('image_uri'); ?>" value="Upload Image" style="margin-top:5px;" />
</p>
<?php
}
}
?>
------------------------------------------------------------------------------
In widget.js:
jQuery(document).ready( function($) {
function media_upload(button_class) {
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$('body').on('click', button_class, function(e) {
var button_id ='#'+$(this).attr('id');
var self = $(button_id);
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(button_id);
var id = button.attr('id').replace('_button', '');
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
$('.custom_media_id').val(attachment.id);
$('.custom_media_url').val(attachment.url);
$('.custom_media_image').attr('src',attachment.url).css('display','block');
} else {
return _orig_send_attachment.apply( button_id, [props, attachment] );
}
}
wp.media.editor.open(button);
return false;
});
}
media_upload('.custom_media_button.button');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment