#32693 closed defect (bug) (worksforme)
wp_enqueue_media() : upload_mimes not filtering
Reported by: | jbonnier | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 2.0 |
Component: | Upload | Keywords: | |
Focuses: | Cc: |
Description
Using the following code, I'm trying to allow only certain image types to be upload/selected with the WordPress media API. So I'm using add_filter on upload_mimes to restrict the mime types allowed. Using get_allowed_mime_types() I get an array containing only the mime types I want. However, when I click the change image button, I'm still able to upload files of mime types not listed before (such as PDF). I've been told that might be a bug, so here I am.
public static function file_uploader( $element_id = null, $multiple = true ) { add_filter( 'upload_mimes', array( 'JBLAB_Utils', 'images_upload_mimes' ) ); var_dump( get_allowed_mime_types() ); /** * outputs: * array(3) { * ["jpg|jpeg|jpe"]=> * string(10) "image/jpeg" * ["gif"]=> * string(9) "image/gif" * ["png"]=> * string(9) "image/png" * } */ $multiple = ( $multiple === true ) ? 'true' : 'false'; wp_enqueue_script('jquery'); wp_enqueue_media(); ?> <div> <?php if ( empty( $element_id ) ) { $element_id = "jblab_uploaded_file_url"; ?> <label for="jblab_uploaded_file_url"><?php _e( 'Image', 'jblab-radionomy' ); ?></label> <input type="text" name="jblab_uploaded_file_url" id="jblab_uploaded_file_url" class="regular-text"> <?php } ?> <input type="button" name="jblab_upload_file_upload_btn" id="jblab_upload_upload_btn" class="button-secondary" value="<?php _e( 'Change Image', 'jblab-radionomy' ); ?>"> </div> <script type="text/javascript"> jQuery(document).ready(function($){ $('#jblab_upload_upload_btn').click(function(e) { e.preventDefault(); var image = wp.media({ title: '<?php echo str_replace( "'", "\'", __( 'Change Image', 'jblab-radionomy' ) ); ?>', multiple: <?php echo $multiple; ?> }).open() .on('select', function(e){ var uploaded_image = image.state().get('selection').first(); var image_url = uploaded_image.toJSON().url; <?php echo " if ($('#$element_id').is('img')) { $('#$element_id').attr('src',image_url); } else { $('#$element_id').val(image_url); } "; ?> }); }); }); </script> <?php } public static function images_upload_mimes ( $mimes = array() ) { //unset( $mimes ); $mimes = array( 'jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', ); return $mimes; }
Attachments (1)
Change History (8)
#2
in reply to:
↑ 1
@
9 years ago
- Keywords reporter-feedback added
- Version changed from 4.2.2 to 2.0
Thanks for the report, @jbonnier.
I'm unable to reproduce this issue. The following code behaves as expected:
function images_upload_mimes( $mimes = array() ) { $mimes = array( 'jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', ); return $mimes; } add_filter( 'upload_mimes', 'images_upload_mimes' );
In your example code, where is the file_uploader
method triggered? Is it hooked onto an action such as init
? If not, that is probably the cause of your problem.
Replying to steve@…:
add_filter('upload_mime_types','remove_pdf');
The correct filter name is upload_mimes
.
#4
@
9 years ago
32693.tests.patch introduces unit test coverage for the upload_mimes
filter.
I tried using
and was able to upload a PDF.