Just found a solution. It's a bit tricky, but works.
As usually create media window, and save it to variable.
var pdfPicker = wp.media({
title: 'Pick a PDF',
button: {
text: 'Use this file...'
},
...
}
Then you need bind to event after input creation and specify required file extensions. Notice that I used once
, to prevent infinite loop of input recreation.
pdfPicker.once('uploader:ready', function () {
var uploader = pdfPicker.uploader.uploader.uploader; // Upload manager
//Updating allowed extensions
uploader.setOption('filters',
{
mime_types: [
{ extensions: "pdf" }
]
}
);
//Trick to reinit field
uploader.setOption('multi_selection', false);
});
If you want to specify more than one extension - separate it with comma.
{ extensions: "pdf,doc,docx" }
Pay attention that file types are hardcoded. You can view listing here:
wp-includes/js/plupload/moxie.js:1283
Update for WP Customizer WP_Customize_Media_Control and WP_Customize_Upload_Control.
First you need to get WP Customizer control that you defined in customize_register hook, after that bind to click/keydown event, and finally just use previously provided solution.
var yourControl = wp.customize.control('you_control_name'),
yourContainer = yourControl.container;
yourContainer.one('click keydown', function () {
if (yourControl.frame) {
var filePicker = yourControl.frame;
filePicker.once('uploader:ready', function () {
var uploader = filePicker.uploader.uploader.uploader;
uploader.setOption('filters',
{
mime_types: [
{ extensions: "doc,docx" }
]
}
);
uploader.setOption('multi_selection', false);
});
}
});
Drawback of this solution, that i don't sure about event execution order. And maybe there can be a point when yourControl.frame will be not defined. In this case i just added if check.
If anyone can clarify that aspect, please post a comment.