如何在 wp.editor 中将库类型图像更改为 pdf


How to change library type image to pdf in wp.editor

我在wp编辑器中使用自定义wp.editor作为自定义帖子类型,我只想上传PDF。但是现在我的上传者可以上传任何类型的文件,但它不会在上传媒体库列表中显示 pdf。

我搜索了很多,但没有任何帮助。这是我的代码。

媒体上传者.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();
    });
});

函数.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" />

请指导我如何做到这一点。

这应该有效:

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' );