打开WordPress 3.5媒体管理器时预选择图像 [英] Pre-Select Images when opening WordPress 3.5 media manager

查看:60
本文介绍了打开WordPress 3.5媒体管理器时预选择图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在与WordPress中的新媒体管理器一起玩耍,并从中获得了一些乐趣,但是已经达到了将我的头撞在墙上的地步.

I've been playing around with the new media manager in WordPress and had some fun with it, but have reached the point where I'm banging my head against a wall.

我有一个自定义的meta框,我想在其中存储一些图像(这是一个隐藏的输入,我当前正在存储其ID,但也可以是图像对象),然后进行AJAX调用以显示一些缩略图,后来我将其拖入了缩略图,以便用户可以重新排序(不一定只与某些背景相关).

I have a custom meta box that I'd like to store some images in (well it's a hidden input and I'm currently storing their ID's, but could equally be the image objects), then making an AJAX call to show some thumbnails, which I have subsequently made draggable so users can reorder (not necessarily relevant just some background).

我的问题是,当我打开媒体管理器时,没有选择任何图像,因此,如果用户要编辑图库中的图片,则需要再次选择所有图片.

My problem is that when I open the media manager, no images are selected, so if a user wants to edit the pictures in their gallery they need to select them all again.

我要弄清楚的是如何通过当前图像打开媒体管理器,以便预先选择它们.

What I'm trying to figure out, is how do I open the media manager with the current images passed through so they are pre-selected.

因此,我的代码大致是这样

So, broadly, my code looks like this

jQuery('#myButton').click(function(e) {
  e.preventDefault();
  frame = wp.media({
    title : 'My Gallery Title',
    multiple : true,
    library : { type : 'image'},
    button : { text : 'Insert' },
  });
  frame.on('close',function() {
    // get selections and save to hidden input plus other AJAX stuff etc.
  }
  frame.open();
});

我的想法是必须有一个参数可以传递到框架中(可能是图像的JSON对象,或者我需要为

My thought is that there must be either a parameter to pass into the frame (probably a JSON object of the images, or I need to create an event for

frame.on('open', function() {
  // Set selected images
}

但是我已经尝试了两种方法,但是却一无所获.

But I have tried both ways round and am not getting anywhere.

这似乎是有可能的,因为更改特征图像"会将您带到选择了当前特征的库中-我只是还无法充分理解核心代码,希望其他人也可以!

It would appear possible, as changing the 'Featured Image' takes you to the library with the current one selected - I've just been unable to understand the core code sufficiently yet and hope someone else has !

推荐答案

在对内核进行了一些研究之后,答案确实非常简单.

After studying the core for a bit, the answer here is really quite straightforward.

收听wp.media对象的打开事件,获取状态,使用ID创建附件对象并将其添加到选择中.

Listen for the open event of the wp.media object, grab the state, create attachment objects with your id's and add them to the selection.

frame.on('open',function() {
  var selection = frame.state().get('selection');
  var ids_value = jQuery('#my_field_id').val();

  if(ids_value.length > 0) {
    var ids = ids_value.split(',');

    ids.forEach(function(id) {
      attachment = wp.media.attachment(id);
      attachment.fetch();
      selection.add(attachment ? [attachment] : []);
    });
  }
});

这在选择多幅图像和单幅图像时都有效,并假定您使用上面的代码将值存储在单个文本/隐藏字段中,并以逗号分隔.

This works when selecting multiple images as well as single and assumes that using the code above you have stored the values in a single text/hidden field with comma separation.

这篇关于打开WordPress 3.5媒体管理器时预选择图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆