1

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.
}
  • is the file is too large, do you want to denied the file or resize it ? to resize it you can try the plugin wordpress.org/plugins/resize-image-after-upload – mmm Sep 30 '16 at 15:32
  • I don't want to use plugin. I just want to control the size and file type of user uploads. – pershianix Sep 30 '16 at 17:07
  • Any suggestion? – pershianix Oct 1 '16 at 17:34
  • Could anyone help me with this please? – pershianix Oct 6 '16 at 11:52
  • I'm still here hoping someone help me :( – pershianix Oct 14 '16 at 9:10
0

Use PHP

// 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' )
){

    // Input type file name
    $image_input_name = 'my_image_upload';

    // Allowed image types
    $allowed_image_types = array('image/jpeg','image/png');

    // Maximum size in bytes
    $max_image_size = 1000 * 1000; // 1 MB (approx)

    // Check if there's an image
    if (isset($_FILES[$image_input_name]['size']) && $_FILES[$image_input_name]['size'] > 0){

        // Check conditions
        if(in_array($_FILES[$image_input_name]['type'], $allowed_image_types)
            && $_FILES[$image_input_name]['size'] <= $max_image_size){

            // You shall pass

            // 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($image_input_name , $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 {
            // You shall not pass
        }
    }
} else {
    // The security check failed, maybe show the user an error.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.