2013-03-07 44 views
0

我試圖將附件添加到沒有編輯器支持(僅摘錄)的自定義帖子類型。將媒體附加到沒有編輯器支持的帖子類型

我已經設法顯示媒體管理器對話框,但我只能看到「插入到發佈」按鈕(反正什麼都不做),上傳圖片時,他們不會附加到帖子。

要實現我做了什麼,到目前爲止,我添加了一個非常簡單的meta框的帖子類型:

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的文檔,我可能會設法得到我想要的,但我找不到任何有用的東西。 我發現的唯一解決方案都依賴於自定義字段,而不是僅僅將這些圖像附加到帖子中,而不將它們插入到帖子內容中,就像我使用普通帖子那樣。

作爲一個側面的問題:是否可以告訴媒體管理器只接受圖像?

回答

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(); 
    }); 
}); 
相關問題