扩展3.5媒体上传插件以更改按钮名称

时间:2013-04-13 作者:souporserious

我已经学习了如何使用Wordpress的3.5媒体上传器上传图像的教程。一切都很好。我只想将实际上传按钮“插入帖子”的文本更改为。。“插入图像”。

我还尝试将其作为一个插件选项来实现,这样它就可以是动态的。

有什么帮助吗?我对jQuery没什么意见,但还没有这么先进的东西。

$.fn.wptuts = function(options) {
   var selector = $(this).selector; // Get the selector
   // Set default options
   var defaults = {
      \'preview\' : \'.preview-upload\',
      \'text\'    : \'.text-upload\',
      \'button\'  : \'.button-upload\',
      \'upload\'  : \'Upload Image\'
   };
   var options = $.extend(defaults, options);

   var _custom_media = true;
   var _orig_send_attachment = wp.media.editor.send.attachment;

   // When the Button is clicked...
   $(options.button).click(function() {
      // Get the Text element.
      var button = $(this);
      var text = $(this).siblings(options.text);
      var send_attachment_bkp = wp.media.editor.send.attachment;

      _custom_media = true;

      wp.media.editor.send.attachment = function(props, attachment) {
         if(_custom_media) {
            // Get the URL of the new image
            text.val(attachment.url).trigger(\'change\');
         } else {
            return _orig_send_attachment.apply(this, [props, attachment]);
         };
      }

      // Change Button Text
      wp.media.frames.file_frame = wp.media({
          title: defaults.upload,
          button: {
              text: defaults.upload
          },
          multiple: false
      });

      wp.media.editor.open(button);

      return false;
   });

   $(\'.add_media\').on(\'click\', function() {
     _custom_media = false;
   });

   $(options.text).bind(\'change\', function() {
      // Get the value of current object
      var url = this.value;
      // Determine the Preview field
      var preview = $(this).siblings(options.preview);
      // Bind the value to Preview field
      $(preview).attr(\'src\', url);
   });
}

// Usage
$(\'.upload\').wptuts(); // Use as default option.

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

用另一种方法解决了这个问题。帮助来自here.

$.fn.oeUpload = function(options) {
    // Set default options
    var defaults = {
      \'preview\' : \'.preview-upload\',
      \'text\'    : \'.text-upload\',
      \'button\'  : \'.button-upload\',
      \'name\'    : \'Choose Image\'
    };
    var options = $.extend(defaults, options);
    var uploader;

    $(options.button).click(function(e) {

        e.preventDefault();

        //If the uploader object has already been created, reopen the dialog
        if (uploader) {
            uploader.open();
            return;
        }

        //Extend the wp.media object
        uploader = wp.media.frames.file_frame = wp.media({
            title: options.name,
            button: {
                text: options.name
            },
            multiple: false
        });

        //When a file is selected, grab the URL and set it as the text field\'s value
        uploader.on(\'select\', function() {
            attachment = uploader.state().get(\'selection\').first().toJSON();
            $(options.text).val(attachment.url);
            $(options.preview).attr(\'src\', attachment.url);
        });

        //Open the uploader dialog
        uploader.open();

    });
}

$(\'.upload\').oeUpload({name: "Choose This Image"});

结束