duanpen9294 2019-08-14 10:01
浏览 278

Wordpress wp.media上传图片

I am trying create my own wordpress widget. I want upload 2 image using selecting from media.

I create 2 input area and 2 buttons. I select image. But 2 input values change together. For open "wp.media" used jQuery code. There is my code. Where is my error ? Can you help me ?

////Form
public function form( $instance ) {

$firstimage = ! empty( $instance['firstimage'] ) ? $instance['firstimage'] : '';

$secondimage = ! empty( $instance['secondimage'] ) ? $instance['secondimage'] : '';


<input class="widefat image-upload" id="<?php echo esc_attr( $this->get_field_id( 'firstimage' ) ); ?>"name="<?php echo esc_attr( $this->get_field_name( 'firstimage' ) ); ?>" type="text" value="<?php echo esc_url( $firstimage ); ?>">
<button type="button" class="button button-primary js-image-upload">Select</button>




<input class="widefat image-upload2" id="<?php echo esc_attr( $this->get_field_id( 'secondimage' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'secondimage' ) ); ?>" type="text" value="<?php echo esc_url( $secondimage ); ?>">
<button type="button" class="button button-primary js-image-upload">Select</button>

<?php 
}


////Update

   public function update( $new_instance, $old_instance ) {
       $instance = array();
       $instance['firstteam'] = ( ! empty( $new_instance['firstteam'] ) ) ? sanitize_text_field( $new_instance['firstteam'] ) : '';
       $instance['firstimage'] = ( ! empty( $new_instance['firstimage'] ) ) ? sanitize_text_field( $new_instance['firstimage'] ) : '';
       $instance['secondteam'] = ( ! empty( $new_instance['secondteam'] ) ) ? sanitize_text_field( $new_instance['secondteam'] ) : '';
       $instance['secondimage'] = ( ! empty( $new_instance['secondimage'] ) ) ? sanitize_text_field( $new_instance['secondimage'] ) : '';

       return $instance;
   }




/////There is my jQuery code:




jQuery(document).ready(function($){

$(document).on('click', '.js-image-upload', function(e){
   e.preventDefault();
   var $button = $(this);
   var file_frame = wp.media.frames.file_frame= wp.media({
       title: 'Klub logotipini tanlang yoki yuklang',
       library:{
           type: 'image',

       },
       button:{
           text: 'Logotipni tanlash'
       },
       multiple: false
   });

   file_frame.on('select', function(){
       var attachment = file_frame.state().get('selection').first().toJSON();
       $button.siblings().val(attachment.url);
       $("input").trigger("input");
   });
   file_frame.open();
});

});
  • 写回答

1条回答 默认 最新

  • douzhi9635 2019-08-14 10:22
    关注

    the click event you are handling is on the class '.js-image-upload'. the class name is same for both the buttons. I think this is causing the issue.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备