WP 3.5媒体上载程序API设置选定项

时间:2013-07-10 作者:Nabeel

我知道如何使用WP 3.5中的新媒体上传程序,并将其与插件和主题集成,但我有一个问题,即用户选择了图像,并在再次打开图像时关闭了我想要的帧用户在库中选择或突出显示之前选择的图像。

codex没有任何api文档

$button.on(\'click\', function(e){
    // prevent default behavior
    e.preventDefault();
    if ( typeof file_frame != \'undefined\' ) {
        file_frame.close();
    }

    // create and open new file frame
    file_frame = wp.media({
        //Title of media manager frame
        title: \'Select an Image\',
        library: {
            type: \'image\'
        },
        button: {
            //Button text
            text: \'Use Image\'
        },
        //Do not allow multiple files, if you want multiple, set true
        multiple: false,
    });

    //callback for selected image
    file_frame.on(\'select\', function() {
        var selected = [];
        if ( is_multiple ) {
            // multiple images selected
            var selection = file_frame.state().get(\'selection\');
            selection.map(function(file) {
                selected.push(file.toJSON());
            });
        } else {
            // single image
            selected.push(file_frame.state().get(\'selection\').first().toJSON());
        }

        // loop through selected images
        for (var i in selected) {
            console.log(selected[i]);
        }

    });

    // open file frame
    file_frame.open();
});
有人能帮忙吗

1 个回复
SO网友:rclai89

我从这里找到了解决方案:

https://stackoverflow.com/questions/13936080/pre-select-images-when-opening-wordpress-3-5-media-manager

这是可行的。这是我的修改,这是使用单个图像选择媒体帧:

frame.on(\'open\', function(){
    var selection = frame.state().get(\'selection\');
    var selected = $(\'#image-id\').val(); // the id of the image
    if (selected) {
        selection.add(wp.media.attachment(selected));
    }
});

结束

相关推荐