• I have a front end form with file upload that limits files to certain types. I need to display an error message if a user submits a file in an unsupported format.

    File upload works correctly and I can limit uploads to certain types using the below function. I’ve added this function to the theme functions.php.

    // limit front end upload to image formats only
    add_filter('upload_mimes', 'image_upload_mimes');
    function image_upload_mimes ( $existing_mimes=array() ) {	
    
    	// remove all mime types
    	unset($existing_mimes);
    	$existing_mimes = array('jpg|jpeg|jpe' => 'image/jpeg',
    							'gif' => 'image/gif',
    							'png' => 'image/png',
    							'bmp' => 'image/bmp',
    							'tif|tiff' => 'image/tiff');
    
    	// and return the new list of mimes
    	return $existing_mimes;
    }

    If the user submits an unsupported format, I would like to display an error message indicating that the format is not supported. I believe that the back end has a built in feature for this.

    How do I detect that an uploaded format is unsupported from the front end?

Viewing 1 replies (of 1 total)
  • Thread Starter hp3

    (@hp3)

    I found the is_wp_error() function to determine if there is a word press error, such as attempting to upload an unsubpported file type, and the get_error_message() function to show the error message.

    if ( $file_err_check === UPLOAD_ERR_OK){ // no error in file upload
        $newupload = insert_attachment($file,$pid); // $newupload returns the attachment id of the file
    
        // check for word press error, like file not supported
        if (is_wp_error($newupload)) {
             $error_string = $newupload->get_error_message();
             echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
        }
    }

    Is there a way to customize the wording of word press errors from a lanaguage file?

Viewing 1 replies (of 1 total)
  • The topic ‘front end form file upload error message for unsupported mime types’ is closed to new replies.