Wordpress自定义上传器未显示附件设置

时间:2014-06-05 15:32:55

标签: wordpress wordpress-plugin wordpress-theming

我一直在寻找这个并没有发现任何与此相关的内容。

我在插件中使用自定义上传器来插入图像。

我正在使用的代码就是这个

$('#upload_image_button').click(function(e) {
    e.preventDefault();
    //If the uploader object has already been created, reopen the dialog
    if (custom_uploader) {
        custom_uploader.open();
        return;
    }
    //Extend the wp.media object
    custom_uploader = wp.media.frames.file_frame = wp.media({
        title: 'Choose Image',
        button: {
            text: 'Choose Image'
        },
        multiple: false
    });
    //When a file is selected, grab the URL and set it as the text field's value
    custom_uploader.on('select', function() {
        attachment = custom_uploader.state().get('selection').first().toJSON();
        console.log(attachment);
        $('.ppwfg-images td').append( "<div id='image-placeholders' style='width:75px; height:75px; margin: 3px; float: left;'>" );
        $('#image-placeholders').append( "<input type='text' id='upload_image' name='ppwfg[]' class='' value=''  size='10' hidden />" );
        $('#image-placeholders').append( "<img src='' id='upload_image_holder' class='ppwfg_image' style='width:75px; height:75px; margin: 3px;'/>" );
        $('#image-placeholders').append( "<a class='close-overlay remove'>x</a>" );

        $('#image-placeholders').attr("id", attachment.id);

        $('#upload_image').attr("value", attachment.url);
        $('#upload_image').attr("class", attachment.id);
        $('#upload_image').attr("id", attachment.id);

        $('#upload_image_holder').attr("src", attachment.url);
        $('#upload_image_holder').attr("class", attachment.id);
        $('#upload_image_holder').attr("id", attachment.id);
    });
    //Open the uploader dialog
    custom_uploader.open();
});

上传器打开,我可以选择一个图像并插入它,但问题是它总是使用主图像,并且没有显示附件设置,下拉选择使用哪个图像......所以我离开了使用默认图像,最终变得很大。

如何在media.frame中显示附件设置?

1 个答案:

答案 0 :(得分:0)

太老了,但我想我会回答一些仍在寻找的人。

custom_uploader = wp.media.frames.file_frame = wp.media({
        title: 'Choose Image',
        button: {
            text: 'Choose Image'
        },
        frame: 'post',
        state:    'insert',
        multiple: false
    });

    //When a file is selected, grab the URL and set it as the text field's value
    custom_uploader.on('insert', function() {

你可以选择frame:post但是你要选择状态:insert并使用函数custom_uploader.on('insert',function(){