2016-02-11 36 views
0

我在wp編輯器中使用自定義wp.editor作爲自定義帖子類型我只想上傳PDF文件。但是現在我的上傳者可以上傳任何類型的文件,但是它不會在上傳媒體庫列表中顯示我的pdf。如何在wp.editor中將庫類型圖像更改爲pdf

我搜索很多,但沒有得到任何幫助。這是我的代碼。

媒體uploader.js

jQuery(document).ready(function($){ 

    // Instantiates the variable that holds the media library frame. 
    var meta_image_frame; 

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

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

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

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

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

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

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

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

}); 

的functions.php

function wm_image_uploader_enqueue() { 
    global $typenow; 
    if(($typenow == 'digital_archives')) { 
     wp_enqueue_media(); 

     wp_register_script('meta-image', get_stylesheet_directory_uri() . '/js/media-uploader.js', array('jquery')); 
     wp_localize_script('meta-image', 'meta_image', 
      array(
       'title' => 'Upload a PDF', 
       'button' => 'Use this PDF', 
      ) 
     ); 
     wp_enqueue_script('meta-image'); 
    } 
} 
add_action('admin_enqueue_scripts', 'wm_image_uploader_enqueue'); 

而metafield代碼

<input type="text" name="wm_txt_issue_pdf" id="wm_txt_issue_pdf" value="<?php echo $wm_txt_issue_pdf; ?>" style="width: 77%"> 
<input type="button" id="wm_issue_pdf_btn" class="button" value="Upload a PDF" /> 

請GUI我怎麼能這樣做。

+0

你得到任何錯誤? – Naumov

+0

有沒有任何錯誤 – deemi

回答

0

這應該工作:

function modify_file_types($file_types) { 

    $file_types['application/pdf'] = array(__('PDFs'), __('Manage PDFs'), _n_noop('PDF <span class="count">(%s)</span>', 'PDFs <span class="count">(%s)</span>')); 

    return $file_types; 

} 

add_filter('post_mime_types', 'modify_file_types'); 

這是做只自定義文章類型上傳:

function wm_image_uploader_enqueue() { 
    global $typenow; 
    if(is_singular('digital_archives')) { 
     wp_enqueue_media(); 

     wp_register_script('meta-image', get_stylesheet_directory_uri() . '/js/media-uploader.js', array('jquery')); 
     wp_localize_script('meta-image', 'meta_image', 
      array(
       'title' => 'Upload a PDF', 
       'button' => 'Use this PDF', 
      ) 
     ); 
     wp_enqueue_script('meta-image'); 
    } 
} 
add_action('admin_enqueue_scripts', 'wm_image_uploader_enqueue'); 
+0

我可以限制它只爲我的digital_archive自定義帖子類型 – deemi

+0

限制上傳或列表視圖? – designtocode

+0

僅限自定義帖子類型的上傳限制 – deemi

相關問題