具有大小选择的WordPress Media Uploader

bbt*_*imx 9 php wordpress jquery plugins image

我想在我自己的WordPress插件中添加一个图像输入.为此,我使用标准的WordPress媒体上传器,如下所示:

var custom_uploader;

$('.upload_image_button').click(function(e) {
    input = $(this);
    e.preventDefault();

    custom_uploader = wp.media.frames.file_frame = wp.media({
        title: 'Choose Collage Image',
        library: {
            type: 'image'
        },
        button: {
            text: 'Choose Collage Image'
        },
        multiple: false,

        displaySettings: true,

        displayUserSettings: false
    });

    custom_uploader.on('select', function() {
        attachment = custom_uploader.state().get('selection').first().toJSON();
        input.prev('input').val(attachment.url);
    });

    custom_uploader.open();

});
Run Code Online (Sandbox Code Playgroud)

这很完美.我添加了两个与我的插件完全相同的图像大小:

if ( function_exists( 'add_image_size' ) ) {
    add_image_size( 'collage-large', 460, 660, true );
    add_image_size( 'collage-small', 460, 325, true );
}
Run Code Online (Sandbox Code Playgroud)

我的问题:图像大小的选择器或更好的缩略图选择器未显示在媒体上传表单中.我怎么做?

StW*_*StW 5

您可以使用媒体插入对话框上的“编辑页面”的网站,这增加了如图所示对齐的link_to尺寸输入字段。为此,请添加frame: 'post'到您的选项数组中:

file_frame = wp.media.frames.file_frame = wp.media({
    title: 'Select a image to upload',
    button: {
        text: 'Use this image',
    },
    multiple: false,
    frame:    'post',    // <-- this is the important part
    state:    'insert',
});
Run Code Online (Sandbox Code Playgroud)

而不是听“选择”事件听“插入”事件。此代码显示如何检索其他属性,包括大小:

// When an image is inserted, run a callback.
file_frame.on( 'insert', function(selection) {
    var state = file_frame.state();
    selection = selection || state.get('selection');
    if (! selection) return;
    // We set multiple to false so only get one image from the uploader
    var attachment = selection.first();
    var display = state.display(attachment).toJSON();  // <-- additional properties
    attachment = attachment.toJSON();
    // Do something with attachment.id and/or attachment.url here
    var imgurl = attachment.sizes[display.size].url;
    jQuery( '#filenameFromURL' ).val( imgurl );
});
Run Code Online (Sandbox Code Playgroud)


mai*_*o84 3

你已经非常接近了。要在管理面板中选择尺寸,请查看add_image_size Codex 条目

add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
    return array_merge( $sizes, array(
        'your-custom-size' => __('Your Custom Size Name'),
    ) );
}
Run Code Online (Sandbox Code Playgroud)

因此,就您而言,这应该可以满足您的需要:

add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
    return array_merge( $sizes, array(
        'collage-large' => __('Collage Large'),
        'collage-small' => __('Collage Small'),
    ) );
}
Run Code Online (Sandbox Code Playgroud)