【发布时间】:2016-02-11 12:20:12
【问题描述】:
我在 wp 编辑器中使用自定义 wp.editor 自定义帖子类型我只想上传 PDF。但是现在我的上传器可以上传任何类型的文件,但它没有在上传媒体库列表中显示 pdf。
我搜索了很多,但没有任何帮助。这是我的代码。
media-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' );
和元字段代码
<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" />
请指导我如何做到这一点。
【问题讨论】:
标签: php jquery wordpress pdf attachment