Limit filetype and wp media

by user3193259   Last Updated June 12, 2019 12:08 PM

This is my input markup:

<div class="button-primary" id="fileToUpload">Upload</div>

This is js:

jQuery( '#fileToUpload' ).click( function()
{
    var custom_uploader = wp.media
    ({
        title: 'Select',
        button: {
            text: 'Select'
        },
        multiple: false  // Set this to true to allow multiple files to be selected.
    })
    .on( 'select', function()
    {
        var attachment = custom_uploader.state().get( 'selection' ).first().toJSON();
        jQuery( '#previewImage' ).attr( 'src', attachment.url );
        jQuery( '.custom_media_url' ).val( attachment.url );
        jQuery( '.custom_media_id' ).val( attachment.id );
    })
    .open();
});

I want to limit the the file type to jpg,jpeg and png. How can i achieve this?



Answers 1


I have shown your questions and i have found correction in it.

You need to update your code as below.

.on( 'select', function()
{
    if(attachment.mime == "image/jpg" || attachment.mime == "image/jpge" || attachment.mime == "image/png")
    {
       var attachment = custom_uploader.state().get( 'selection' ).first().toJSON();
       jQuery( '#previewImage' ).attr( 'src', attachment.url );
       jQuery( '.custom_media_url' ).val( attachment.url );
       jQuery( '.custom_media_id' ).val( attachment.id );
    }
    else
    {
        alert("Please select jpg,jpeg and png type images only");
        custom_uploader.open();
        return;
    }
})

Thanks you!

vaghela chetan
vaghela chetan
June 12, 2019 11:50 AM

Related Questions





Use wordpress media selector in tinymce popup

Updated February 18, 2017 21:08 PM