0

我正在尝试将附件添加到没有编辑器支持的自定义帖子类型(仅摘录)。

我已经设法显示了媒体管理器对话框,但我只能看到“插入帖子”按钮(无论如何它什么都不做),并且在上传图像时,它们不会附加到帖子中。

为了实现我到目前为止所做的事情,我在帖子类型中添加了一个非常简单的元框:

function add_gallery_post_media_meta_box()
{

    add_meta_box(
        'gallery_post_media',
        'Gallery Media',
        'gallery_post_media',
        'gallery',
        'side',
        'high'
    );

} // add_file_meta_box
add_action('add_meta_boxes', 'add_gallery_post_media_meta_box');

function gallery_post_media()
{

    echo '<a href="#" id="gallery-add-media" title="' . __('Add media') .'">' . __('Add media') .'</a>';

} // end post_media

function register_admin_scripts() {
    wp_enqueue_media();
    wp_register_script( 'gallery_post_media_admin_script', get_template_directory_uri() . '/library/cpt/gallery.js' );
    wp_enqueue_script( 'gallery_post_media_admin_script' );

} // end register_scripts
add_action( 'admin_enqueue_scripts', 'register_admin_scripts' );

和脚本:

jQuery(document).ready(function ($) {
    $('#gallery-add-media').click(function (e) {
        var send_attachment_bkp = wp.media.editor.send.attachment;
        var button = $(this);
        var id = button.attr('id').replace('_button', '');
        wp.media.editor.send.attachment = function (props, attachment) {
            $("#" + id).val(attachment.url);
            wp.media.editor.send.attachment = send_attachment_bkp;
        }

        wp.media.editor.open(button);
        event.preventDefault();
        return false;
    });
});

如果我能找到一些关于 的文档wp.media.editor.send.attachment,我可能会设法得到我想要的,但我找不到任何有用的东西。我发现的唯一解决方案都依赖于自定义字段,相反,我只想将这些图像附加到帖子中,而不像处理普通帖子那样将它们插入帖子内容中。

作为一个附带问题:是否可以告诉媒体管理器只接受图像?

4

1 回答 1

1

这是我用于媒体字段的 JavaScript。一旦你点击插入,你就可以对所选图像中的数据做任何你想做的事情

jQuery(document).ready(function() {
   //uploading files variable
   var custom_file_frame;
   jQuery(document).on('click', '.meida-manager', function(event) {
      event.preventDefault();
      $this = jQuery(this);
      //If the frame already exists, reopen it
      if (typeof(custom_file_frame)!=="undefined") {
         custom_file_frame.close();
      }

      //Create WP media frame.
      custom_file_frame = wp.media.frames.customHeader = wp.media({
         //Title of media manager frame
         title: "Sample title of WP Media Uploader Frame",
         library: {
            type: 'image'
         },
         button: {
            //Button text
            text: "insert text"
         },
         //Do not allow multiple files, if you want multiple, set true
         multiple: false
      });

      //callback for selected image
      custom_file_frame.on('select', function() {
         var attachment = custom_file_frame.state().get('selection').first().toJSON();
         //do something with attachment variable, for example attachment.filename
         //Object:
         //attachment.alt - image alt
         //attachment.author - author id
         //attachment.caption
         //attachment.dateFormatted - date of image uploaded
         //attachment.description
         //attachment.editLink - edit link of media
         //attachment.filename
         //attachment.height
         //attachment.icon - don't know WTF?))
         //attachment.id - id of attachment
         //attachment.link - public link of attachment, for example ""http://site.com/?attachment_id=115""
         //attachment.menuOrder
         //attachment.mime - mime type, for example image/jpeg"
         //attachment.name - name of attachment file, for example "my-image"
         //attachment.status - usual is "inherit"
         //attachment.subtype - "jpeg" if is "jpg"
         //attachment.title
         //attachment.type - "image"
         //attachment.uploadedTo
         //attachment.url - http url of image, for example "http://site.com/wp-content/uploads/2012/12/my-image.jpg"
         //attachment.width
         $this.val(attachment.url);
         $this.siblings('img').attr('src',attachment.url);
      });

      //Open modal
      custom_file_frame.open();
   });
});
于 2013-09-03T22:14:09.943 回答