A day before tomorrow, I developed a custom widget for my Recent WordPress theme. In that widget, have an image upload field with some other fields. I have done everything and the widget is working very well except one problem. When I am dragging the widget in sidebar and giving the information needed in all fields, its showing fine in front-end. But after that when I am trying to change the image from that widget back-end then the save button remain saved. Image doesn’t change.
Codes are below:
class Themeslug_About_Widget extends WP_widget{
public function __construct(){
parent::__construct('author_info', esc_html__( 'About Info Box', 'blogista' ), array(
'description' => esc_html__( 'About Info Box contain brief about Author/ Company.', 'blogista' ),
));
}
public function widget( $args, $instance ){
echo $args['before_widget'];
echo $args['before_title'] . $instance['title'] . $args['after_title'];
?>
<img src="<?php echo $instance['author_box_image']; ?>" alt="<?php echo $instance['title']; ?>" />
<div class="widget-content">
<h3 class="title">
<a href="#"><?php echo $instance['author_name']; ?></a>
</h3>
</div>
<?php
echo $args['after_widget'];
}
public function form( $instance ){
$title = '';
if( !empty( $instance['title'] ) ) {
$title = $instance['title'];
}
$author_box_image = '';
if( ! empty( $instance['author_box_image'] ) ) {
$author_box_image = $instance['author_box_image'];
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'blogista' ); ?></label>
<input type="text" value="<?php echo esc_attr( $title ); ?>" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" class="widefat">
</p>
<p>
<button class="button button-primary" id="author_info_image"><?php _e( 'Upload Image', 'blogista' ); ?></button>
<input type="hidden" name="<?php echo $this->get_field_name('author_box_image'); ?>" id="<?php echo $this->get_field_id('author_box_image'); ?>" class="image_link" value="<?php echo esc_url( $author_box_image ); ?>" >
<div class="image_show">
<img src="<?php echo $instance['author_box_image']; ?>" width="200" height="auto" alt="">
</div>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? esc_attr( $new_instance['title'] ) : '';
$instance['author_box_image'] = ( ! empty( $new_instance['author_box_image'] ) ) ? esc_url( $new_instance['author_box_image'] ) : '';
return $instance;
}
}
function themeslug_admin_enqueue_scrits(){
wp_enqueue_media();
wp_enqueue_script( 'admin_custom_script', get_theme_file_uri() . '/js/libs/admin_scripts.js', array( 'jquery' ), '1.0', true );
}
add_action( 'admin_enqueue_scripts', 'themeslug_admin_enqueue_scrits' );
Then added the below jQuery code:
(function($){
$(document).ready(function(){
$('button#author_info_image').on("click",function( e ){
e.preventDefault();
var imageUploader = wp.media({
'title' : 'Upload Author Image',
'button' : {
'text' : 'Set The Image'
},
'multiple' : false
});
imageUploader.open();
imageUploader.on("select", function(){
var image = imageUploader.state().get("selection").first().toJSON();
var link = image.url;
$("input.image_link").val( link );
$(".image_show img").attr('src', link);
});
});
});
}(jQuery))
Every thing is working fine first time but when trying to change the image then Widget save button remain saved. Please Help.
Thanks in Advance.