微件保存后无法打开微件媒体上载程序

时间:2017-07-14 作者:Alexander

我试图用WordPress媒体上传器简化我的小部件。当我点击按钮时Upload 它打开了媒体上传程序,但在我点击save, 该按钮不再工作,如果我有一个活动的小部件,然后添加另一个,同样的行为,媒体上传按钮在第二个小部件中不工作。控制台中没有JS错误。有人能指出我错在哪里吗?

PHP

class Inka_Profile_Widget extends WP_Widget {

  // setup the widget name, description etc.
  function __construct() {
    $widget_options = array(
      \'classname\'   => esc_attr( "inka_profile_widget", \'inka\' ),
      \'description\'  => esc_html__( \'Custom Profile Widget\', \'inka\' ),
      \'customize_selective_refresh\' => true
    );
    parent::__construct( \'inka_profile\', \'Inka Profile\', $widget_options);
  }  


  // back-end display of widget
  function form( $instance ) {

    $title = ! empty( $instance[\'title\'] ) ? $instance[\'title\'] : esc_html__( \'About Me\', \'inka\' );
    $image = ! empty( $instance[\'image\'] ) ? $instance[\'image\'] : \'\';

    ?>

      <p>
        <label for="<?php echo esc_attr( $this->get_field_id(\'title\') ); ?>"><?php esc_attr_e( \'Title\', \'inka\' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( \'title\' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( \'title\' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
      </p>
      <p>
        <img src="<?php echo esc_attr( $image ); ?>" alt="" class="deo-image-holder" style="width: 100%;">
      </p>      
      <p>
        <input type="hidden" class="deo-image-hidden-field" name="<?php echo $this->get_field_name( \'image\' ); ?>" id="<?php echo $this->get_field_id( \'image\' ); ?>" value="<?php echo esc_attr( $image ); ?>"/>
        <input type="button" class="deo-image-upload-button button button-primary" value="Upload">      
        <input type="button" class="deo-image-delete-button button" value="Remove Image">
      </p>

    <?php
  }

  // front-end display of widget
  function widget( $args, $instance ) {
    echo "Hey";
  }


  // update of the widget
  function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
    $instance[\'image\'] = ( ! empty( $new_instance[\'image\'] ) ) ? strip_tags( $new_instance[\'image\'] ) : \'\';
    return $instance;
  }

}


add_action( \'widgets_init\', function() {
  register_widget( \'Inka_Profile_Widget\' );
});

JS

(function($){

  /* WordPress Media Uploader
  -------------------------------------------------------*/
  var addButton = $(\'.deo-image-upload-button\');
  var deleteButton = $(\'.deo-image-delete-button\');
  var hiddenField = $(\'.deo-image-hidden-field\');
  var imageHolder = $(\'.deo-image-holder\');
  var mediaUploader;


  addButton.on(\'click\', function(e) {
    e.preventDefault();
    if ( mediaUploader ) {
      mediaUploader.open();
      return;
    }

    mediaUploader = wp.media.frames.file_frame = wp.media({
      title: \'Select an Image\',
      button: {
        text: \'Use This Image\'
      },
      multiple: false
    });

    mediaUploader.on(\'select\', function() {
      var attachment = mediaUploader.state().get(\'selection\').first().toJSON();
      hiddenField.val(attachment.url);
      imageHolder.attr(\'src\', attachment.url);
    });

    mediaUploader.open();

  });  


})(jQuery);

2 个回复
最合适的回答,由SO网友:Alexander 整理而成

好的,我终于找到了导致问题的原因,并想在这里分享我的解决方案,以防有人有同样的经历。令人遗憾的是,这些信息没有在任何地方突出显示(codex、wp博客、stackexchange等)。

第一个问题是如何将click事件绑定到按钮。如果您直接在按钮选择器上绑定它,保存小部件后WordPress将刷新它并从该按钮取消绑定click事件。因此,这里的解决方案是单击主体,然后将其委托给按钮选择器:

$(\'body\').on(\'click\', \'.deo-image-upload-button\', function(e) {
    e.preventDefault();
    ...
}
第二个问题是,小部件保存后,隐藏的输入不再填充所选图像的url。这是因为我如何缓存inputimage mediaUploader外部变量中的元素select 事件只需将这些元素放在内部即可解决此问题:

mediaUploader.on(\'select\', function() {
    var attachment = mediaUploader.state().get(\'selection\').first().toJSON();      
    $(\'.deo-hidden-input\').val(attachment.url);
    $(\'.deo-media-image\').attr(\'src\', attachment.url);
});
因此,经过所有清理和优化后,我的代码现在看起来如下所示:

/* WordPress Media Uploader
  -------------------------------------------------------*/
  var mediaUploader = wp.media.frames.file_frame = wp.media({
    title: \'Select an Image\',
    button: {
      text: \'Use This Image\'
    },
    multiple: false
  });

  $(\'body\').on(\'click\', \'.deo-image-upload-button\', function(e) {
    e.preventDefault();

    if ( mediaUploader ) {
      mediaUploader.open();
    }

    mediaUploader.on(\'select\', function() {
      var attachment = mediaUploader.state().get(\'selection\').first().toJSON();      
      $(\'.deo-hidden-input\').val(attachment.url);
      $(\'.deo-media-image\').attr(\'src\', attachment.url);
    });

    mediaUploader.open();

  });
希望这些信息对您有所帮助;)

SO网友:Ramachandran Annamalai

我试过这个代码,效果很好

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

   $(document).on("click", ".fla_upload_image_button", function (e) {
      e.preventDefault();
      var $button = $(this);


      // Create the media frame.
      var file_frame = wp.media.frames.file_frame = wp.media({
         title: \'Select or upload image\',
         library: { // remove these to show all
            type: \'image\' // specific mime
         },
         button: {
            text: \'Select\'
         },
         multiple: false  // Set to true to allow multiple files to be selected
      });

      // When an image is selected, run a callback.
      file_frame.on(\'select\', function () {
         // We set multiple to false so only get one image from the uploader

         var attachment = file_frame.state().get(\'selection\').first().toJSON();

         $button.siblings(\'input\').val(attachment.url);

      });

      // Finally, open the modal
      file_frame.open();
   });
});

结束

相关推荐

Responsive Images

是否有本机功能可以根据屏幕分辨率显示帖子缩略图或图库图像的正确响应图像大小?通常我使用:1、自定义图像大小:function customImageSetup () { add_theme_support( \'post-thumbnails\' ); add_image_size(\'grid_1 mini square\', 60, 60, TRUE); add_image_size(\'grid_2\', 160); add_image_size(\'