$this->get_field_id() not working in customizer

Question

$this->get_field_id() is working fine when widgets are already added to it but when I add widgets using customizer it returns wrong id something like #widget-i

My Code:

//Custom Widgets

class howl_iconbox extends WP_Widget  {

function __construct() {
parent::__construct(
'howl_iconbox',
__('Features Box', 'materiality'),
array( 'description' => __( 'HowlThemes- Add this widget in only featured section of customizer', 'materiality' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
echo __( 'Hello, World!', 'materiality' );
echo $args['after_widget'];
}

public function form( $instance ) {
if(isset($instance[ 'icon' ])){
$icon = $instance[ 'icon' ];
}else{
  $icon = "";
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'icon' ); ?>"><?php _e( 'Choose icon:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'icon' ); ?>" name="<?php echo $this->get_field_name( 'icon' ); ?>" type="text" value="<?php echo esc_attr( $icon ); ?>" />
</p>
<script>
jQuery(document).ready(function() {
  jQuery("#<?php echo $this->get_field_id( 'icon' ); ?>").iconpicker("#<?php echo $this->get_field_id( 'icon' ); ?>");
});
</script>
<?php
}

// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['icon'] = ( ! empty( $new_instance['icon'] ) ) ? strip_tags( $new_instance['icon'] ) : '';
return $instance;
}
} // Class wpb_widget ends here

// Register and load the widget
function wpb_load_widget() {
    register_widget( 'howl_iconbox' );
}
add_action( 'widgets_init', 'wpb_load_widget' );

I’m using inputbox id to iconpicker function but problem is when we add widgets using customizer it is returning wrong id.

<script>
jQuery(document).ready(function() {
  jQuery("#<?php echo $this->get_field_id( 'icon' ); ?>").iconpicker("#<?php echo $this->get_field_id( 'icon' ); ?>");
});
</script>
0
, Aumkar Thakur 5 months 0 Answers 57 views 0

Leave an answer