The ability to limit the types of files that can be uploaded via the File field, similar to how the Media Uploader can be limited. I'd expect it would look something like this:
'options' => array(
'url' => false, // Hide the text input for the url,
'extensions' => array('jpg', 'png', 'mp3', 'pdf'),
),
Not sure, can't find documentation on this anywhere yet.
Ideally, I'd love to limit this field to support ONLY PDFs.
$compositions_meta->add_field(array(
'name' => esc_html__('Orchestration', 'compositions'),
'desc' => esc_html__('Upload your Composition Orchestration file as a PDF', 'compositions'),
'id' => $prefix . 'pdf_file',
'type' => 'file',
'options' => array(
'url' => false, // Hide the text input for the url
),
'text' => array(
'add_upload_file_text' => 'Add PDF' // Change upload button text. Default: "Add or Upload File"
),
));
I added an example to the wiki for specifying file types for a file (or file_list) field: https://github.com/WebDevStudios/CMB2/wiki/Field-Types#file
Awesome, that rocks. Thanks!
'query_args' => array( 'type' => 'application/pdf', ),
works for limiting a selection from the library, but it doesn't limit a selection when uploading a file.
Is there another method to restrict what can be uploaded to the library?
Not sure that's something that would be easy to code up, especially as a possible parameter for a field.
This does offer some interesting points about the overall details on such a problem https://stackoverflow.com/questions/4328947/limit-file-format-when-using-input-type-file
It'd be interesting to see how to possibly do any sort of server-side validation before processing the upload and saving a value.
I'm using CMB2 on a new project and it's a bit frustrating that the query_args on this field can be very easily circumvented while uploading, unless there's another argument I'm not seeing or a usage strategy I'm misunderstanding.
Even with query_args set, a user can still upload a restricted file type and then immediately click "Use this file" in the Media Uploader, which disregards any restrictions.
Just for comparison, this is something ACF handles quite well.
After some quick searching, it seems some validation of uploads may be possible using the wp_handle_upload_prefilter filter.
Hi, how can i get the url file to display it into an anchor tag (Text
Thanks!
@franmip if you mean that your field is storing a url, and you simply want to make it used as a link, simply echo the field out as the href value.
<a href="<?php esc_attr( $myurl ); ?>">link text</a>
Escaping on the link for added security :)
@tw2113 thanks so much!!
Most helpful comment
I'm using CMB2 on a new project and it's a bit frustrating that the
query_argson this field can be very easily circumvented while uploading, unless there's another argument I'm not seeing or a usage strategy I'm misunderstanding.Even with
query_argsset, a user can still upload a restricted file type and then immediately click "Use this file" in the Media Uploader, which disregards any restrictions.Just for comparison, this is something ACF handles quite well.
Edit
After some quick searching, it seems some validation of uploads may be possible using the
wp_handle_upload_prefilterfilter.