Skip to content

Instantly share code, notes, and snippets.

@saroarhossain57
Created December 1, 2016 16:38
Show Gist options
  • Save saroarhossain57/7355a1f7bfd21dfbebb58261f4430f46 to your computer and use it in GitHub Desktop.
Save saroarhossain57/7355a1f7bfd21dfbebb58261f4430f46 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Colornews Image Upload
Plugin URI: http://bobl.com.bd/
Description: It is a simple plugin that adds a widget to your favourite sidebar to upload a single image and show it in sidebar location with custom url whatever you want
Version: 1.0
Author: BOBL
Author URI: http://bobl.com.bd/
*/
?>
<?php
class colornewsImageUploader extends WP_Widget
{
/**
* Widget construction
*/
function __construct()
{
$widget_ops = array('classname' => 'widget_image image-upload-widget', 'description' => __('Image, link'));
$control_ops = array('width' => 278);
parent::__construct('colornewsImageUploader', __('Fixed Advertise Image Upload', 'colornews'), $widget_ops, $control_ops);
}
public function widget( $args, $instance )
{
if (!isset($args['widget_id'])) {
$args['widget_id'] = null;
}
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance);
$imageUri = empty($instance['image_uri']) ? '' : $instance['image_uri'];
$url = empty($instance['url']) ? '' : $instance['url'];
}
/**
* Run on widget update
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['image_uri'] = strip_tags($new_instance['image_uri']);
$instance['url'] = strip_tags($new_instance['url']);
return $instance;
}
/**
* Setup the widget admin form
*/
public function form( $instance )
{
$instance = wp_parse_args( (array) $instance, array(
'title' => '',
'image_uri' => '',
'url' => '',
));
$title = $instance['title'];
$imageUri = $instance['image_uri'];
$url = $instance['url'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'colornews'); ?>:</label><br />
<input type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" value="<?php echo $title ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('image_uri'); ?>"><?php _e('Image', 'colornews'); ?>:</label><br />
<input type="text" class="img" name="<?php echo $this->get_field_name('image_uri'); ?>" id="<?php echo $this->get_field_id('image_uri'); ?>" value="<?php echo $imageUri; ?>" />
<input type="button" class="select-img" value="Select Image" />
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>"><?php _e('Link', 'colornews'); ?>:</label><br />
<input type="text" name="<?php echo $this->get_field_name('url'); ?>" id="<?php echo $this->get_field_id('url'); ?>" value="<?php echo $url; ?>" class="widefat" />
</p>
<?php
}
}
// queue up the necessary js
function imgupl_enqueue()
{
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_script('hrw', plugin_dir_url( __FILE__ ).'/js/script.js', null, null, true);
}
add_action('admin_enqueue_scripts', 'imgupl_enqueue');
/**
* Register the widget
*/
function colornews_Image_Upload_Widget_init() {
register_widget('colornewsImageUploader');
}
add_action('widgets_init', 'colornews_Image_Upload_Widget_init');
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&amp;TB_iframe=true');
return false;
});
window.send_to_editor = function(html) {
imgurl = $('img', html).attr('src');
image_field.val(imgurl);
tb_remove();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment