jshock
jshock

Reputation: 1503

How can I get Wordpress to allow uploading .doc and .docx

I am writing a custom Wordpress plugin. I want to be able to upload .pdf, .doc, and .docx files. PDFs are already accepted by default, however I cannot get DOC or DOCX files to upload. First I tried adding them as new mime types:

function allowDocMimeTypes( $mime_types ) {
    $mime_types = array (
        'doc' => 'application/msword',
        'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    );
    return $mime_types;
}
$new_mimes = add_filter( 'upload_mimes', 'allowDocMimeTypes' );

That did not work. Next I tried adding them as overrides to the wp_handle_upload() function:

$upload_overrides = array( 
    'test_form' => false,
    'mimes' => array(
        'pdf' => 'application/pdf',
        'doc' => 'application/msword',
        'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    ),
);
$movefile = wp_handle_upload( $file, $upload_overrides );

That doesn't work either. Any suggestions?

Upvotes: 0

Views: 1944

Answers (1)

Jasper B
Jasper B

Reputation: 881

Your first solution was the correct one however you where overwriting the existing values. the below code should work without overwriting existing ones.

function mime_types($mimes) {
  $mimes['doc'] = 'application/msword';
  $mimes['docx'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
  return $mimes;
}
add_filter('upload_mimes', 'mime_types');

enter image description here

Upvotes: 1

Related Questions