如何在自定义媒体上传器WordPress中激活媒体库

时间:2017-02-20 07:16:32

标签: php jquery wordpress

我制作自定义媒体加载器并且它在元字段上完美地工作。在我的媒体加载器中没有画廊选项。但现在我想为它提供一个选项。我经常搜索并按照他们的步骤但没有成功。

这是我的代码,在没有图库选项的情况下完美运行:

var meta_image_frame_gallery;

    // Runs when the image button is clicked.
    jQuery('#additional_image_1').click(function(e){

        // Prevents the default action from occuring.
        e.preventDefault();

        // If the frame already exists, re-open it.
        if ( meta_image_frame_gallery ) {
            meta_image_frame_gallery.open();
            return;
        }

        // Sets up the media library frame
        meta_image_frame_gallery = wp.media.frames.meta_image_frame_gallery = wp.media({
            title: 'Upload Image',
            button: { text:  'Upload Image' },
            library: { type: 'image' },
        });

         Runs when an image is selected.
        meta_image_frame_gallery.on('select', function(){

            // Grabs the attachment selection and creates a JSON representation of the model.
            var media_attachment = meta_image_frame_gallery.state().get('selection').first().toJSON();

            // Sends the attachment URL to our custom image input field.
            jQuery('#itv_additional_image_1').val(media_attachment.url);
        });


        // Opens the media library frame.
        meta_image_frame_gallery.open();
    });

这是我的图库选项代码:

var meta_image_frame_gallery;

    // Runs when the image button is clicked.
    jQuery('#additional_image_1').click(function(e){

        // Prevents the default action from occuring.
        e.preventDefault();

        // If the frame already exists, re-open it.
        if ( meta_image_frame_gallery ) {
            meta_image_frame_gallery.open();
            return;
        }

        // Sets up the media library frame
        meta_image_frame_gallery = wp.media.frames.meta_image_frame_gallery = wp.media({
            title: 'Upload Image',
            button: { text:  'Upload Image' },
            multiple: true,
            library: { type: 'image' },
        });

        // Runs when an image is selected.
        //meta_image_frame_gallery.on('select', function(){
        //
        //    // Grabs the attachment selection and creates a JSON representation of the model.
        //    var media_attachment = meta_image_frame_gallery.state().get('selection').first().toJSON();
        //
        //    // Sends the attachment URL to our custom image input field.
        //    jQuery('#itv_additional_image_1').val(media_attachment.url);
        //});

        // When an image is selected, run a callback.
        meta_image_frame_gallery.on( 'select', function() {

            var selection = meta_image_frame_gallery.state().get('selection');

            selection.map( function( attachment ) {

                attachment = attachment.toJSON();

                // Do something with attachment.id and/or attachment.url here
                jQuery('#itv_additional_image_1').val(selection.url);
            });
        });

        // Opens the media library frame.
        meta_image_frame_gallery.open();
    });

首先我想要一个库选项,第二个我创建一个库时,它的短代码将显示在元字段上,然后我将它保存在数据库中。

请给予任何帮助我的指导。

1 个答案:

答案 0 :(得分:1)

我通过更多搜索来找到它,并发现一些链接检查它们。现在图库选项在我的媒体加载器中处于活动状态。

现在是我的代码: 再添加三个参数

frame: "post", state: 'gallery-library', multiple: true

在此行meta_image_frame_gallery = wp.media.frames.wp_media_frame = wp.media( {

完整代码:

meta_image_frame_gallery = wp.media.frames.wp_media_frame = wp.media( {
            title: 'My Gallery',
            frame: "post",
            state: 'gallery-library',
            library: {
                type: 'image'
            },
            multiple: true
        } );

现在它工作得很好