limit file type and file size using media_handle_upload

Question

I have a front-end form that by using it, users can submit posts from front-end. everything is working great but I have one final problem.

In my form I have a file uploader and I’m using media_handle_upload for uploading user files and it is working fine but I’m wondering if there is a way to limit the file type and file size in media_handle_upload?

I know that media_handle_upload handle these! but how can I change the default behavior?

Here is my code for upload processing:

// Check that the nonce is valid, and the user can edit this post.
if ( 
    isset( $_POST['my_image_upload_nonce'] ) 
    && wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
) {
// The nonce was valid and the user has the capabilities, it is safe to continue.

// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );

// Let WordPress handle the upload.
// Remember, 'my_image_upload' is the name of our file input in our form above.
$attachment_id = media_handle_upload( 'my_image_upload', $post_id );

if ( is_wp_error( $attachment_id ) ) {
    // There was an error uploading the image.
} else {
    add_post_meta($post_id, 'post_image', $attachment_id, true);
}

} else {

    // The security check failed, maybe show the user an error.
}
0
, , pershianix 9 months 0 Answers 95 views 0

Leave an answer