【问题标题】:How to change library type image to pdf in wp.editor如何在 wp.editor 中将库类型图像更改为 pdf
【发布时间】: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


    【解决方案1】:

    这应该可行:

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

    【讨论】:

    • 我可以限制它只用于我的 digital_archive 自定义帖子类型
    • 限制上传或列表查看?
    • 仅限自定义帖子类型上传
    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2018-06-02
    • 2012-01-21
    • 1970-01-01
    • 2014-03-21
    相关资源
    最近更新 更多