0

I have created a wordpress media uploader using the code below however this only seems to allow only images. How can I modify this to accept document files such as PDF or Word documents?

file_frame = wp.media.frames.file_frame = wp.media({
        title: $( this ).data( 'uploader_title' ),
        frame: 'select',
        button: {
            text: $( this ).data( 'uploader_button_text' ),
        },
        multiple: false // set this to true for multiple file selection
    });

You assistance is highly appreciated.

Bryan L
  • 80
  • 8

2 Answers2

2

Try this one for uploading document using media upload

function wpse_59621_mimes_filter( $mimes ) {
    return array( 'pdf' => 'application/pdf' );
}

function wpse_59621_delay_mimes_filter( $value ) {
    if ( isset( $_REQUEST['post_id'] ) && get_post_type( $_REQUEST['post_id'] ) === 'my_post_type' )
        add_filter( 'upload_mimes', 'wpse_59621_mimes_filter' );
    else
        remove_filter( 'upload_mimes', 'wpse_59621_mimes_filter' );

    return $value;
}

add_filter( 'wp_handle_upload_prefilter', 'wpse_59621_delay_mimes_filter' );
PPL
  • 6,357
  • 1
  • 11
  • 30
  • The `wp_handle_upload_prefilter` didn't work for me so I just used the `upload_mimes` directly and it worked perfectly. – Bryan L May 19 '18 at 06:23
0

I got the respective mime types from here.

function wpse_59621_mimes_filter( $mimes ) {
    $mimes['doc']  = 'application/msword';
    $mimes['pdf']  = 'application/pdf';
    $mimes['docx'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
    $mimes['xls'] = 'application/vnd.ms-excel';
    $mimes['xlt'] = 'application/vnd.ms-excel';
    $mimes['xla'] = 'application/vnd.ms-excel';
    $mimes['xlsx'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';

    return $mimes;
}
add_filter( 'upload_mimes', 'wpse_59621_mimes_filter' );
Bryan L
  • 80
  • 8